Android中的onCompletion没有完成媒体播放器跟踪的结束

时间:2013-07-28 11:09:29

标签: android eclipse

我意识到我可能遗漏了一些显而易见的东西,但基本上我已经创建了一个播放轨道的MediaPlayer,并且在最后我希望媒体播放器停止,重置并将Seekbar显示回零,但是当它到达轨道的末尾,onCompletion方法似乎不起作用 - 基本上,搜索栏不会回到零。搜索栏正常更新,所以我很确定它与更新搜索栏的方法无关,而是与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();


}

1 个答案:

答案 0 :(得分: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);