2011/03/21

Android assets內音效撥放

不能用 MediaPlayer
MediaPlayer不能撥Assets內資源
會出現 java.io.IOException: Prepare failed.: status=0xFFFFFFFC
但可以以Assets內資源弄一個暫存檔到sdcard中撥放即可

不用上面方法可改用SoundPool,大概如下
AssetFileDescriptor afd = this.getResources().getAssets().openFd(file_name);
SoundPool soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
int soundRunIdx = soundPool.load(afd, 0);

soundPool.play(soundRunIdx, 1, 1, 0, 0, 1);

限制: 1M記憶體,僅適用於短音效

ref:

沒有留言 :