因此,我使用audiofiles ID ann陣列。但它取決於用戶將有多少個ID(他鍵入「a」,並在數組中出現R.raw.a)。如何使用MediaPlayer播放這個聲音。我想我應該使用OnCompletionListener,但我不知道有多少個聲音ID將在數組中。我應該使用如何一個一個地播放audiofiles
for(){}
因此,我使用audiofiles ID ann陣列。但它取決於用戶將有多少個ID(他鍵入「a」,並在數組中出現R.raw.a)。如何使用MediaPlayer播放這個聲音。我想我應該使用OnCompletionListener,但我不知道有多少個聲音ID將在數組中。我應該使用如何一個一個地播放audiofiles
for(){}
可以設置爲的MediaPlayer OnCompletionListener
public class MyActivity extends Activity
implements MediaPlayer.OnCompletionListener {
MediaPlayer player = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
player = new MediaPlayer();
player.setOnCompletionListener(this);
...
}
@Override
public void onCompletion(MediaPlayer mp) {
// Called when playback is complete
//Play a next song
...
}
}
歡迎您!
使用IntentService播放歌曲。所有使用intentservice開始的歌曲將排隊等候。它會一個接一個地玩。
你可以做的是
//define a variable to be used as index.
int audioindex = 0;
//Extract the files id into an array
int[] audioFileIds=new int[]{R.raw.as,R.raw.bs};
然後在你的MediaPlayer
onCompletionListener
把類似以下。
然後在您的OnCompletionListener中。
mp.setOnCompletionListener(new OnCompletionListener(){
// @Override
public void onCompletion(MediaPlayer player) {
// File has ended, play the next one.
FunctionPlayFile(audioFileIds[audioindex]);
audioindex+=1; //increment the index to get the next audiofile
}
});
但我不知道將有多少audiofiles將在陣列中。 – user1437450