[英]Loading SoundPool with audio from APK Expansion file
在文档中说:
如果您使用扩展文件存储媒体文件,则ZIP文件仍然允许您使用提供偏移和长度控件(例如MediaPlayer.setDataSource()和SoundPool.load())的Android媒体播放调用。
我有10个小的.ogg文件,我想使用SoundPool.load(FileDescriptor fd,长偏移,长长度,int优先级)来加载这些音频文件。 我已经写了一些代码,但是当我运行它时,我收到错误消息:
“无法加载样本:(空)”
,并且显然不会播放音频。 这是我尝试的:
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()之后; 而要播放它需要几秒钟的加载时间。 但是代码可以解决这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.