2013-10-09 95 views
2

我試圖使用MediaPlayer一個接一個播放多個媒體文件。我試圖播放文件的目錄有大約20個文件,我寫的代碼只播放三個文件,然後停止播放。請指出我的錯誤並幫助我解決它。Android:在使用MediaPlayer之後播放多文件文件

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 

        @Override 
        public void onCompletion(MediaPlayer mp) { 
         mediaPlayer.stop(); 
         System.out.println("coming here to playNextMEdia"); 
         playNextMedia(); 
        }  
       }); 
      } 

private void playNextMedia() { 
     System.out.println("playNextMedia "+files.size()+"and"+counter); 
     if(counter<files.size()){ 
      mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.fromFile(files.get(counter))); 

      mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 

       @Override 
       public void onCompletion(MediaPlayer mp) { 
        mediaPlayer.stop(); 
        System.out.println("playNextFile() cehck 1"); 

        //playNextFile(); 
        //System.out.println("playNextFile() cehck 2"); 
counter++; 
        playNextMedia(); 
       } 

      }); 

logcat的

10-09 21:49:10.129: I/System.out(2644): coming here to playNextMEdia 
10-09 21:49:10.129: I/System.out(2644): playNextMedia 19and1 
10-09 21:49:36.249: I/System.out(2644): playNextFile() cehck 1 
10-09 21:49:36.249: I/System.out(2644): playNextMedia 19and2 

也許,我缺少一個小條件檢查什麼的。

編輯1

if (fd != null) { 
       try { 
        mediaPlayer.reset(); 
        mediaPlayer.setDataSource(fd); 
        //mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.fromFile(files.get(counter))); 
        mediaPlayer.setScreenOnWhilePlaying(true); 


       } catch (IllegalStateException e) { 
        e.printStackTrace(); 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       mediaPlayer.start(); 
+0

而不是使用'創建()'創建新MediaPlayers所有的時間,我會用的setDataSource:HTTP:/ /developer.android.com/reference/android/media/MediaPlayer.html#setDataSource(java.lang.String) –

+0

@KenWolf我已經嘗試過使用setDataSource也重置mediaPlayer也,它沒有做任何好的 – onkar

+0

你在哪裏打電話' 。開始()'? –

回答

0

嘗試是這樣的:

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 

    @Override 
    public void onCompletion(MediaPlayer mp) { 
     counter++; 
     if (counter < files.size()) { 
      mediaPlayer.setDataSource(files.get(counter)); 
      mediaPlayer.prepare(); 
      mediaPlayer.start(); 
     } 
    }  
}); 
相關問題