简体   繁体   English

如何使用Mediaplayer循环播放-Android

[英]How to loop using mediaplayer - Android

How do i play my audio files in sequence from my sdcard? 如何从sdcard顺序播放音频文件? I have two classes, one for results another for the actual rendering. 我有两个类,一个用于结果,另一个用于实际渲染。 Is my while loop in the correct place? 我的while循环是否在正确的位置?

public void DoIt() {  
while(!mp.isPlaying()){


AudioRenderer mr = new AudioRenderer();
mp = mr.AudioRenderer(filePath);
if(mp!=null){
mp.start(); 
if(!mp.isPlaying())
    break;
}
}

if(mp == null){ *write results logic*}



private class AudioRenderer extends Activity {
private MediaPlayer AudioRenderer(String filePath) {


MediaPlayer mp = new MediaPlayer();
File location = new File(filePath);

Uri path = Uri.fromFile(location);
ExtentionSeperator media = new ExtentionSeperator(filePath, '.');
if(ext.equals("mp3") || ext.equals("wav")|| ext.equals("ogg")|| ext.equals("mid")|| 
ext.equals("flac")){


        mp= MediaPlayer.create(this, path);}
                 return mp;}

My app just keeps writing the results without waiting for the first one to stop playing first. 我的应用程序一直保持写入结果,而无需等待第一个停止播放。 I want it to Render -> Play -> Wait for whole audio to stop playing -> Write result into file -> Next file. 我希望它呈现->播放->等待整个音频停止播放->将结果写入文件->下一个文件。

if you want them to play one after the other 如果你想让他们一个接一个地玩

there is an event in mediaplayer 媒体播放器中有一个事件

mp.setOnCompletionListener()

which will fire once the first file is complete. 一旦第一个文件完成,它将触发。 here you can play the next file. 在这里您可以播放下一个文件。

try this code 试试这个代码

class MediaDemo extends Activity{
  public static MediaPlayer myplayer=new MediaPlayer();
  public static ArrayList<String> pathlist=new ArrayList<String>();
   public void onCreate(Bundle savedInstanceState){
       myplayer.reset();
       myplayer.setOnCompletionListener(new OnCompletionListener(){
         public void onCompletion(MediaPlayer arg0) {
             pathlist.remove(0);
             if(pathlist.size()>=1){
                 myplayer.reset();
                 playAudio();
            }
         }
     });
 pathlist.add("filename");
 if(!myplayer.isPlaying()){
    playAudio();
 }
}
public void playAudio(){
     try{
         if(pathlist.size()>=1){
           String path=pathlist.get(0);
            myplayer.setDataSource(path);
            myplayer.prepare();
            myplayer.start();
         }
     }catch(Exception e){e.printStackTrace();}
} 

} }

according to your code you have to use the mp.setOnCompletionListener(). 根据您的代码,您必须使用mp.setOnCompletionListener()。 In this give path for the next file which you want to play. 在此提供您要播放的下一个文件的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM