您可以創建單獨的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();
}
}
我試着你的建議,得到一個錯誤MediaPlayer mp = new MediaPlayer(this,myMusic [0]);'帶有「構造函數MediaPlayer(mediatest,int)是未定義的。」我工作過但仍然沒有成功。我做錯了什麼? – Splitusa 2011-06-15 18:36:20
對不起。應該是'MediaPlayer mp = MediaPlayer.create(this,myMusic [0]);'我會修復代碼 – 2011-06-15 19:15:18
Im有點困惑於你所建議的代碼的順序,因爲我得到錯誤「can not解決變量「 nd是否意味着'public void startNextFile(){'? – Splitusa 2011-06-16 02:39:20