在文档中它说:
如果您使用扩展文件来存储媒体文件,ZIP 文件仍然允许您使用提供偏移和长度控制的 Android 媒体播放调用(例如 MediaPlayer.setDataSource() 和 SoundPool.load())。
我有 10 个小的 .ogg 文件,我想使用 SoundPool.load (FileDescriptor fd, long offset, long length, int priority) 来加载这些音频文件。我写了一些代码,但是当我运行它时,我收到错误消息:
“无法加载样本:(空)”
,显然没有播放音频。这是我尝试的:
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);}
}
}