0

我意识到我可能遗漏了一些明显的东西,但基本上我已经创建了一个 MediaPlayer 它将播放一个曲目,在它结束时我希望媒体播放器停止,重置并将 Seekbar 显示回零,但是当它到达轨道结束时,onCompletion 方法似乎不起作用 - 基本上,seekbar 不会回到零。seekbar 更新正常,所以我很确定这与更新 seekbar 的方法无关,与 onCompletion 有关。请问有什么建议吗?

公共类 RelaxPlayer 扩展 Activity 实现 OnCompletionListener、SeekBar.OnSeekBarChangeListener {

    private Handler mHandler = new Handler();;
    private Utilities utils;

    private MediaPlayer myMediaPlayer;
    private SeekBar songProgressBar;
    private TextView songCurrentDurationLabel;
    private TextView songTotalDurationLabel;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_relax_player);
    songProgressBar = (SeekBar) findViewById(R.id.seekBar1);

    songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
    songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);

    // Mediaplayer
    myMediaPlayer = new MediaPlayer();

    utils = new Utilities();

    // Listeners
    songProgressBar.setOnSeekBarChangeListener(this); 
    myMediaPlayer.setOnCompletionListener(this); // 



    myMediaPlayer = MediaPlayer.create(RelaxPlayer.this, R.raw.bounce);


    Button myButtonOne = (Button) findViewById(R.id.button1);
    myButtonOne.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (myMediaPlayer.isPlaying()){
                if(myMediaPlayer!=null){
                    myMediaPlayer.pause();
                    //add code to change button text
                }
            } else{
                if(myMediaPlayer!=null){

            myMediaPlayer.start();
            songProgressBar.setProgress(0);
            songProgressBar.setMax(100);
            updateProgressBar();
            //add code to change text
                }
        }
            //more code that I won't copy here...but later
          @Override
public void onCompletion(MediaPlayer arg0) {

   myMediaPlayer.pause();

   myMediaPlayer.seekTo(0);

   songProgressBar.setProgress(0);// this then calls method which will update the seek bar
   updateProgressBar();


}
4

1 回答 1

0

您正在使用以下行重新创建媒体播放器:

 myMediaPlayer = MediaPlayer.create(RelaxPlayer.this, R.raw.bounce);

因此 setOnCompletionListener 用于不同的媒体播放器实例。

你可能想要:

    utils = new Utilities();

    // Listeners
    songProgressBar.setOnSeekBarChangeListener(this);

    myMediaPlayer = MediaPlayer.create(this, R.raw.bounce);
    myMediaPlayer.setOnCompletionListener(this);

    Button myButtonOne = (Button) findViewById(R.id.button1);
于 2013-07-28T12:41:15.027 回答