2011-06-15 67 views
1

我目前正在嘗試創建一個for循環,它將播放一個原始文件,當它完成時,它將繼續到數組中的下一個聲音文件。它目前正在播放所有文件。任何關於如何解決這個問題的建議?我認爲完成監聽器可能存在問題,但不確定。謝謝。for循環使用原始音樂數組android

我的代碼:

package com.example.mediatest; 



    int[] myMusic = {R.raw.caralarm, R.raw.phonebusysignal, R.raw.phoneoffhook}; 



    MediaPlayer mp = new MediaPlayer(); 



    for (int y =0; y<3; y++){ 
     mp = MediaPlayer.create(this, myMusic[y]); 
     mp.start(); 
     mp.setOnCompletionListener(new OnCompletionListener(){ 

     @Override 
     public void onCompletion(MediaPlayer mp) { 
      // TODO Auto-generated method stub 

     } 

     }); 
    } 

回答

5

您可以創建單獨的MediaPlayer對象,先啓動第一個然後在onCompletion開始下一個。對於擴展的解決方案,試試這個來代替:

int[] myMusic = {R.raw.caralarm, R.raw.phonebusysignal, R.raw.phoneoffhook}; 
int mCompleted = 0; 

MediaPlayer mp = MediaPlayer.create(this, myMusic[0]); 

mp.setOnCompletionListener(new OnCompletionListener() { 
    @Override 
    public void onCompletion(MediaPlayer mp) { 
     mCompleted++; 
     mp.reset(); 
     if (mCompleted < myMusic.length()) { 
      try { 
       AssetFileDescriptor afd = getResources().openRawResourceFd(myMusic[mCompleted]); 
       if (afd != null) { 
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
        afd.close(); 
        mp.prepare(); 
       } 
      } catch (Exception ex) { 
       // report a crash 
      } 
     } else { 
      // done with media player 
      mp.release(); 
      mp = null; 
     } 
    } 
}); 

mp.start(); 

更簡單的方法(稍微浪費資源)是:

int[] myMusic = {R.raw.caralarm, R.raw.phonebusysignal, R.raw.phoneoffhook}; 
int mNext; 
OnCompletionListener mListener = new OnCompletionListener() { 
    @Override 
    public void onCompletion(MediaPlayer mp) { 
     mp.release(); 
     startNextFile(); 
    } 
}; 

@Override 
public void onCreate() { 
    // usual onCreate stuff here, then... 

    // either here or whenever you want to start the sequence 
    mNext = 0; 
    startNextFile(); 
} 

void startNextFile() { 
    if (mNext < myMusic.length) { 
     MediaPlayer mp = MediaPlayer.create(this, myMusic[mNext++]); 
     mp.setOnCompletionListener(mListener); 
     mp.start(); 
    } 
} 
+0

我試着你的建議,得到一個錯誤MediaPlayer mp = new MediaPlayer(this,myMusic [0]);'帶有「構造函數MediaPlayer(mediatest,int)是未定義的。」我工作過但仍然沒有成功。我做錯了什麼? – Splitusa 2011-06-15 18:36:20

+0

對不起。應該是'MediaPlayer mp = MediaPlayer.create(this,myMusic [0]);'我會修復代碼 – 2011-06-15 19:15:18

+0

Im有點困惑於你所建議的代碼的順序,因爲我得到錯誤「can not解決變量「 nd是否意味着'public void startNextFile(){'? – Splitusa 2011-06-16 02:39:20

0

會更容易創建MediaPlayers數組,因爲你只有3人。您應該只啓動第一個並設置onCompletionListeners,您將在其中啓動下一個玩家。希望這可以幫助。

+0

這是一個好主意。我現在就開始工作,讓你知道它是如何工作的 – Splitusa 2011-06-15 18:37:12

+0

這就是我想出來的'MediaPlayer mp1 = MediaPlayer.create(this,myMusic [0]); MediaPlayer mp2 = MediaPlayer.create(this,myMusic [1] ); MediaPlayer mp3 = MediaPlayer.create(this,myMusic [2]); final MediaPlayer [] mediaMp = {mp1,mp2,mp3}; mediaMp [0] .start(); mediaMp [0] .setOnCompletionListener(new OnCompletionListener(){@ Override public void onCompletion(MediaPlayer mp){mediaMp [1] .start(); mediaMp [1] .setOnCompletionListener(new OnCompletionListener(){public void onCompletion(MediaPlayer mp){ mediaMp [2] .start();}'的事情是,它的工作原理,但必須有一個更短的方式 – Splitusa 2011-06-15 18:59:46

0

這是在播放歌曲的工作代碼繼續循環。

public class MainActivity extends Activity 
{ 
private int[] tracks = {R.raw.explosion,R.raw.pianothingy_one,R.raw.car_horn_x}; 
int mCompleted = 0; 

public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

MediaPlayer mp = MediaPlayer.create(this, tracks[0]); 
mp.setOnCompletionListener(new OnCompletionListener() 
{ 
    @Override 
    public void onCompletion(MediaPlayer mp) 
    { 
     mCompleted++; 
     mp.reset(); 
     if (mCompleted < tracks.length) 
     { 
      try 
      { 
       AssetFileDescriptor afd = getResources().openRawResourceFd(tracks[mCompleted]); 
       if (afd != null) 
       { 
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
        afd.close(); 
        mp.prepare(); 
        mp.start(); 
       } 
      } 
      catch (Exception ex) 
      { 
       ex.printStackTrace(); 
      } 

     } 
     else if (mCompleted>=tracks.length) 
     { 
      mCompleted =0; 
      try 
      { 
       AssetFileDescriptor afd = getResources().openRawResourceFd(tracks[mCompleted]); 
       if (afd != null) 
       { 
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
        afd.close(); 
        mp.prepare(); 
        mp.start(); 
       } 
      } 
      catch (Exception ex) 
      { 
       ex.printStackTrace(); 
      } 
     } 
     else 
     { 
      mCompleted=0; 
       mp.release(); 
       mp = null; 
     } 

    } 
}); 

mp.start();