是的,Soundpool可能會有幫助,如果你想有這個過程中的播放/暫停。
首先創建一個保存在活動實例中的聲音管理器,以及一個將音頻文件鏈接到ID的映射。
// Replace 10 with the maximum of sounds that you would like play at the same time.
SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 100);
HashMap<String, Integer> stringToSoundID = new HashMap<String, Integer>();
HashMap<Integer, Integer> soundIdToStreamID = new HashMap<Integer, Integer>();
Integer streamIDbeingPlayed= -1;
然後將所有聲音加載到soundPool中,保持文件和聲音ID之間的鏈接。
for(String filePath: Files) {
int soundID = soundPool.load(filePath, 1);
stringToSoundID.put(filePath, soundID);
}
然後你可以有你的功能,播放/暫停像這樣的文件:
void playFile(String filePath) {
if(streamIDbeingPlayed!= -1) {
soundPool.pause(streamIDbeingPlayed);
}
Integer soundID = stringToSoundID.get(filePath);
Integer streamID = soundIdToStreamID.get(soundID);
if(streamID == null) {
streamIDbeingPlayed = soundPool.play (soundID, 1, 1, 1, -1, 1);
soundIdToStreamID.put(soundID, streamIDbeingPlayed);
} else {
soundPool.resume(streamID);
streamIDbeingPlayed = streamID;
}
}
感謝您的回答。但是,當我們使用soundpool時,它不會將播放的當前位置和音頻的總持續時間返回給我。這些細節我用於更新進度條... – developerXXX 2013-05-08 09:20:30
SoundPool用於小型音頻文件。 – 2016-01-13 06:39:49