从APK扩展文件加载音频的Soundpool [英] Loading SoundPool with audio from APK Expansion file
问题描述
在文档它说:
如果您使用的扩展文件存储多媒体文件,ZIP文件
还可以让你使用Android的媒体播放调用,提供
偏移量和长度的控制(如MediaPlayer.setDataSource()和
SoundPool.load())。
块引用>我有10个小型的.ogg文件,我想用SoundPool.load(FD FileDescriptor的,长的偏移量,长度长,INT优先)来加载这些音频文件。我已经写了一些code,但是当我运行它,我得到错误信息:
无法加载示例:(空)
块引用>,显然音频不会播放。以下是我尝试:
公共类MainActivity延伸活动{ 的Soundpool声音;
布尔加载= FALSE;
INT流ID;
INT theId; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
System.gc()的;
的setContentView(R.layout.activity_main);
听起来=新的Soundpool(10,AudioManager.STREAM_MUSIC,0);
sounds.setOnLoadCompleteListener(新SoundPool.OnLoadCompleteListener(){
@覆盖
公共无效的onLoadComplete(SP的Soundpool,诠释SID,诠释状态){
加载=真实的;
}});
loadSounds();
playSound(theID);
} 私人无效loadSounds(){
AssetFileDescriptor描述符= NULL;
尝试{
描述= getFileDescriptor(这一点,audio_files / end_credits.ogg);
}赶上(例外五){
} {最后
如果(描述符!= NULL)
尝试{descriptor.close();}赶上(IOException异常五){}
}
theId = sounds.load(descriptor.getFileDescriptor(),descriptor.getStartOffset(),descriptor.getLength(),1);
}
公共静态AssetFileDescriptor getFileDescriptor(上下文CTX,字符串路径){
AssetFileDescriptor描述符= NULL;
尝试{
子presourceFile邮编= APKExpansionSupport.getAPKExpansionZipFile(CTX,4,-1);
描述= zip.getAssetFileDescriptor(路径);
}赶上(IOException异常五){
e.printStackTrace();
}
返回描述;
}
私人无效playSound(INT soundID){
如果(装入){
流ID = sounds.play(soundID,1,1,1,0,1);}
}
}
解决方案好吧,好像原因是音频没有加载速度不够快。我把playSound(theID);右后loadSounds();而它需要几秒钟加载它才能进行播放。但是,code为工作到这一点。
In the documentation it says:
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()).
I have 10 small .ogg files and I want to use SoundPool.load (FileDescriptor fd, long offset, long length, int priority) to load these audio files. I've written some code, but when I run it I get error message:
"Unable to load sample: (null)"
, and obviously the audio isn't played. Here is what I attempted:
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);} } }
解决方案Ok, seems like the reason is audio isn't loading fast enough. I put playSound(theID); right after loadSounds(); whereas it needs couple seconds to load before it can be played. But the code is working up to that point.
这篇关于从APK扩展文件加载音频的Soundpool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!