Android媒体播放器中的Oncompletion Listener

时间:2013-04-15 06:05:08

标签: java android

我想一个接一个地播放MediaPlayer,我可以获得第一个文件,但无法获取剩余的文件......

此代码是否正确......

private void playRecording()  {

    player = new MediaPlayer();

    player.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub
            player.stop();
            if (i < numberOfFiles) {

                i++;
                playAudio(Environment.getExternalStorageDirectory()
                        .getAbsolutePath()
                        + "/android"
                        + i
                        + ".3gp");
            }else i=0;
        }


    });
    player.start();

playAudio:

  private void playAudio(String filename) {
      try {
      player.setDataSource(filename); 
      player.prepare();
      player.start();
  } catch (Exception e) { 
      // TODO Auto-generated catch block
      e.printStackTrace(); 
      } 
      }

2 个答案:

答案 0 :(得分:3)

使用以下内容更改OnCompletionListener

player.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub
            mp.stop();
            mp.release();
            if (i < numberOfFiles) {
                i++;
 String path=Environment.getExternalStorageDirectory() .getAbsolutePath()+ "/android"+"/"+i+".3gp";
           playAudio(path);
            }
            else i=0;
        }

答案 1 :(得分:3)

代替致电mp.release();最好拨打mp.reset();

其中release将释放对象,重置就像创建对象一样。