一旦媒体播放器被重置并初始化,就不会调用OnCompletionListener

时间:2015-03-18 13:30:32

标签: android android-mediaplayer

在我的代码中,我有一个开始/停止按钮。我正在尝试播放原始文件夹中的音频剪辑。完成音频剪辑​​后,开始/停止按钮必须从停止模式更改为启动模式。我不想循环播放音频。为此,我调用了OnCompletionListener。但是,监听器只被调用一次。如果重置媒体播放器并再次创建,则不再调用OnCompletionListener。

public void playsong(View view) {
        if (mySound != null)

        {

         if (!pause) {mySound.start();
         btnStartStop.setBackgroundResource(R.drawable.button);
         pause = true;




            } else {


                if (mySound.isPlaying()) {


                    mySound.stop();

                    do_it();
                }

            }
        }
    }

 mySound.setOnCompletionListener(new OnCompletionListener() {
            @Override

            public void onCompletion(MediaPlayer mySound) {


                do_it();

            }
        });
public void do_it() {
        mySound.reset();
        mySound = MediaPlayer.create(this, R.raw.a);
        pause = false;
        btnStartStop.setBackgroundResource(R.drawable.button2);

    }

1 个答案:

答案 0 :(得分:0)

试试这个代码? 在这个例子中,每次调用oncompletionlistner

btnDemo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(objPlayer!=null){
            if(objPlayer.isPlaying())
                stopPlaying(objPlayer);
        }
        objPlayer = MediaPlayer.create(getApplicationContext(),R.raw.demoaudio);
        if(objPlayer.isPlaying())
            objPlayer.pause();
        else
            objPlayer.start();
        Log.d("null", "Media Player started!");
        if(objPlayer.isLooping() != true){
            Log.d("null", "Problem in Playing Audio");
        }
        objPlayer.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                objPlayer.release();
                objPlayer = null;
            };
        });

    }
});