我有一堆声音,分配给一组按钮,我需要播放它们。我所有的声音都在资产文件夹中。但是,它不起作用。目的是: 从assetFodler 加载并播放声音。我将从我的项目中拿出代码示例:
//set up audio player
mSoundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//getting files lists from asset folder
aMan = this.getAssets();
try {
filelist = aMan.list("");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
为了没有很多代码行,我创建了一个加载和播放声音的基本程序:
public void loadSound (String strSound, int stream) {
try {
stream= mSoundPool.load(aMan.openFd(strSound), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
}
如您所见,我传递了文件(字符串名称)和流ID。
最后,这是我如何使用它:
case R.id.button1:
//if button was clicked two or more times, when play is still on im doing stop
mSoundPool.stop(mStream1);
loadSound(filelist[0],mStream1);
break;
当我运行项目时,什么也没有发生,logcat 说:
12-09 10:38:34.851: W/SoundPool(17331): sample 2 not READY
任何帮助,将不胜感激。
UPD1:当我这样做时,没有 loadSound 过程它可以正常工作,以下代码是 onCreate:
//load fx
try {
mSoundPoolMap.put(RAW_1_1, mSoundPool.load(aMan.openFd(filelist[0]), 1));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
和 Onclick 按钮:
//resourcePlayer.stop();
mSoundPool.stop(mStream1);
mStream1= mSoundPool.play(mSoundPoolMap.get(RAW_1_1), streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
我只是不想有这么多的代码行,我想让它看起来不错