私は現在SoundPoolライブラリを使用しています。 これで私は異なるサウンドエフェクトを同時に再現できますが、それが音楽の最良の方法であるかどうかわかりません。
郵便関連:Play sound using soundpool example
のMediaPlayerとCyclicBarrierをを使用して編集 :
public enum MP_COMMAND {
START,
STOP,
PAUSE
}
/**
* Uses threads to execute synced commands for the current video media player and
* background music player in tandem.
*/
public void syncedCommand(MediaPlayer player1, MediaPlayer player2, MP_COMMAND command) {
final CyclicBarrier commandBarrier = new CyclicBarrier(2);
new Thread(new SyncedCommandService(commandBarrier, player1, command)).start();
new Thread(new SyncedCommandService(commandBarrier, player2, command)).start();
}
/**
* Inner class that starts a given media player synchronously
* with other threads utilizing SyncedStartService
*/
private class SyncedCommandService implements Runnable {
private final CyclicBarrier mCommandBarrier;
private MediaPlayerTest.MP_COMMAND mCommand;
private MediaPlayer mMediaPlayer;
public SyncedCommandService(CyclicBarrier barrier, MediaPlayer player, MediaPlayerTest.MP_COMMAND command) {
mCommandBarrier = barrier;
mMediaPlayer = player;
mCommand = command;
}
@Override public void run() {
try {
mCommandBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
switch (mCommand) {
case START:
mMediaPlayer.start();
break;
case STOP:
mMediaPlayer.stop();
break;
case PAUSE:
mMediaPlayer.pause();
break;
default:
break;
}
}
}
を両方の曲を再生するには、これを呼び出す:
MediaPlayer mCurrentVideoPlayer = MediaPlayer.create(this, R.raw.track1);
MediaPlayer mBackgroundMusic = MediaPlayer.create(this, R.raw.track2);
syncedCommand(mCurrentVideoPlayer, mBackgroundMusic, MP_COMMAND.START);
参考:Playing multiple songs with MediaPlayer at the same time: only one is really playing
おそらく関連している:http://gamedev.stackexchange.com/questions/104893/sounds-overlapping-when-playing-too-many-at-the-same-timeとhttp://stackoverflow.com/questions/15742812/ android-gdx-sound-performance-issue –