gpt4 book ai didi

android - 从 Assets 中加载和播放音池

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:04:50 25 4
gpt4 key购买 nike

我有一堆声音,分配给一组按钮,我需要播放它们。我所有的声音都在 Assets 文件夹中。但是,它不起作用。 目的是: 从 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);
}

如您所见,我传递了 file(stringName) 和 streamID。

最后,这是我的使用方法:

     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

如有任何帮助,我们将不胜感激。

更新 1:当我这样做时,没有 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();
}

和点击按钮:

  //resourcePlayer.stop();
mSoundPool.stop(mStream1);
mStream1= mSoundPool.play(mSoundPoolMap.get(RAW_1_1), streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);

我只是不想有那么多代码行,我想让它看起来好看

最佳答案

在使用 SoundPool.setOnLoadCompleteListener 播放之前,您需要检查文件是否已成功加载

将您的 loadSound 方法代码更改为:

public void loadSound (String strSound, int stream) {
boolean loaded = false;
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
try {
stream= mSoundPool.load(aMan.openFd(strSound), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Is the sound loaded already?
if (loaded) {
mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
}
}

关于android - 从 Assets 中加载和播放音池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13785078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com