使用Android MediaPlayer同时播放3个音频

时间:2015-10-03 15:16:54

标签: android audio android-mediaplayer media

我正在尝试播放3个音频宽度的Android而不是同时播放,我的代码是下一个:

                for (Word w : wordsList){
                    String path = Parameters.PATH_AUDIO + "/" + w.getAudioPath();
                    Uri audioPath = Uri.parse(path);
                    MediaPlayer audio1 = MediaPlayer.create(context, audioPath);
                    audio1.start();
                }

但是所有的音频都至少在同一时间开始播放。我需要audio1.wait()audio1.sleep或类似的人。

1 个答案:

答案 0 :(得分:0)

您可能需要考虑使用setOnCompletionListener:

audio1.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer player) {

      //do something when audio is finished   

}});

这是一个例子(虽然没有机会尝试):

在课程级别对这些进行Decalre:

int index = 0;
MediaPlayer audio1;

然后:

try {
        audio1 = MediaPlayer.create(this, Uri.parse(yourList.get(index)));
        audio1.start();
        index++;

        audio1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer player) {

                if (index < yourList.size()) {
                    audio1.reset();
                    audio1 = MediaPlayer.create(MainActivity.this,
                            Uri.parse(yourList.get(index)));
                    audio1.start();
                    index++;
                }
                else
                {
                    //release it
                }

            }
        });

    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }