2

我想一个接一个地播放 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();

播放音频:

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

2 回答 2

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;
        }
于 2013-04-15T06:50:39.980 回答
3

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

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

于 2015-02-04T06:39:43.890 回答