gpt4 book ai didi

android - 使用APK扩展文件中的音频加载SoundPool

转载 作者:行者123 更新时间:2023-12-02 23:53:11 24 4
gpt4 key购买 nike

documentation中说:

If you're using your expansion files to store media files, a ZIP file still allows you to use Android media playback calls that provide offset and length controls (such as MediaPlayer.setDataSource() and SoundPool.load()).



我有10个小的.ogg文件,我想使用SoundPool.load(FileDescriptor fd,长偏移,长长度,int优先级)来加载这些音频文件。我已经写了一些代码,但是当我运行它时,我收到错误消息:

"Unable to load sample: (null)"



,并且显然不会播放音频。这是我尝试的:
public class MainActivity extends Activity {

SoundPool sounds;
boolean loaded = false;
int streamID;
int theId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.gc();
setContentView(R.layout.activity_main);
sounds = new SoundPool(10, AudioManager.STREAM_MUSIC,0);
sounds.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool sp, int sid, int status) {
loaded = true;
}});
loadSounds();
playSound(theID);
}

private void loadSounds() {
AssetFileDescriptor descriptor = null;
try {
descriptor = getFileDescriptor(this, "audio_files/end_credits.ogg");
} catch (Exception e) {
} finally {
if (descriptor != null)
try{descriptor.close();} catch (IOException e) {}
}
theId = sounds.load(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength(), 1);
}
public static AssetFileDescriptor getFileDescriptor(Context ctx, String path) {
AssetFileDescriptor descriptor = null;
try {
ZipResourceFile zip = APKExpansionSupport.getAPKExpansionZipFile(ctx, 4, -1);
descriptor = zip.getAssetFileDescriptor(path);
} catch (IOException e) {
e.printStackTrace();
}
return descriptor;
}
private void playSound(int soundID){
if(loaded){
streamID = sounds.play(soundID, 1, 1, 1, 0, 1);}
}
}

最佳答案

好的,似乎原因是音频加载速度不够快。我放了playSound(theID);就在loadSounds()之后;而需要几秒钟才能加载它才能播放。但是代码可以解决这一点。

关于android - 使用APK扩展文件中的音频加载SoundPool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21299892/

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