在文档 ,它说:
如果您使用您的扩展文件存储多媒体文件,ZIP文件还允许您使用提供偏移Android的媒体播放和通话长度控件(如MediaPlayer.setDataSource()和SoundPool.load())。
我有10个小型的.ogg文件,我想用SoundPool.load(FD FileDescriptor的,长期偏移,长度长,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);}
}
}