Play audio file from the assets directory

AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

註: 如果看到 log 裡有以下 error msg "Should have subtitle controller already set",不用太緊張,參考 Should have subtitle controller already set Mediaplayer error Android,基本上不管他也不會怎樣,就是一個 informational 的設定而已。
另外,除了 MediaPlayer, 也可以比較一下 SoundPool, 詳見 【Android遊戲開發之八】詳解MediaPlayer與SoundPool的利弊


comments powered by Disqus
Copyright © 2013 Andro Chen
Powered by Logdown and Greyshade
Favicon from The Noun Project