我有一個遊戲,我使用單獨的MediaPlayer每種聲音的MediaPlayer停止播放任何
這就是:
public class SoundSample {
public final static String CORRECT="sounds/correct.wav";
public final static String PASS="sounds/pass.wav";
public final static String CLOCK_OUT="sounds/clock_out.mp3";
public final static String ALARM="sounds/alarm.mp3";
public final static String COUNTDOWN_TO_START="sounds/countdown_to_start.mp3";
public final static String GO="sounds/go.mp3";
private String assetFile;
private MediaPlayer mediaPlayer;
public SoundSample(String sound){
assetFile = sound;
try {
AssetFileDescriptor afd = MainActivity.activity.getAssets().openFd(sound);
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void play(){
if (mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
mediaPlayer.start();
}
public void release(){
mediaPlayer.release();
mediaPlayer.reset();
mediaPlayer = null;
}
}
所以,我用
correctSound = new SoundSample(SoundSample.CORRECT);
然後 correctSound.play();
因此,第二次使用它會停止在許多手機上播放,再玩一次。我的意思是,調試器顯示mediaPlayer.start();
被執行,但聲音從不再播放。 它工作得很好,不過,我每次測試的Android手機6上,所以它可能只是一個機器人6或5問題
有沒有什麼辦法可以解決這個?我知道我可以使用的Soundpool,但它也有一些問題
哦,你的意思是,設定數據源每次?那不是太多的快速聲音的工作嗎? – user2976267
這是正確的。我只是想添加到這個答案。 @ user2976267你真的需要'MediaPlayer'嗎?有許多獨立的'MediaPlayers'將會對系統造成很大影響。如果你的聲音很小,你可以使用'SoundPool'來處理很多小的聲音文件。 – Ekalips
@ user2976267閱讀這個http://stackoverflow.com/questions/13527134/audiotrack-soundpool-or-mediaplayer-which-should-i-use(埃爾維斯波波維奇的答案) – Ekalips