[英]Media player that plays an mp3 from assets folder throws Exception
I am trying to make my app use an alert sound through the following code but i get an IOException 我正在尝试通过以下代码使我的应用使用警报声音,但出现IOException
private void playSound() {
try {
AssetFileDescriptor afd = getAssets().openFd("sounds/alert.mp3");
MediaPlayer mediaPlayer = new MediaPlayer();
Log.d(TAG, "playSound: mediaplayer created....................");
mediaPlayer.setDataSource(afd.getFileDescriptor());
// MediaPlayer reads the file and gets ready to play
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
The stack trace is the following 堆栈跟踪如下
10-22 22:46:47.380 8612-8612/com.example.android.timer E/MediaPlayer: Unable to to create media player
10-22 22:46:47.400 8612-8612/com.example.android.timer W/System.err: java.io.IOException: setDataSourceFD failed.: status=0x80000000
10-22 22:46:47.430 8612-8612/com.example.android.timer W/System.err: at android.media.MediaPlayer.setDataSource(Native Method)
10-22 22:46:47.430 8612-8612/com.example.android.timer W/System.err: at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1032)
10-22 22:46:47.430 8612-8612/com.example.android.timer W/System.err: at com.example.android.timer.MainActivity.playSound(MainActivity.java:147)
10-22 22:46:47.430 8612-8612/com.example.android.timer W/System.err: at com.example.android.timer.MainActivity.access$000(MainActivity.java:23)
10-22 22:46:47.430 8612-8612/com.example.android.timer W/System.err: at com.example.android.timer.MainActivity$1.onPlayNotification(MainActivity.java:52)
10-22 22:46:47.430 8612-8612/com.example.android.timer W/System.err: at com.example.android.timer.MyTimer.run(MyTimer.java:113)
10-22 22:46:47.430 8612-8612/com.example.android.timer W/System.err: at android.os.Handler.handleCallback(Handler.java:615)
10-22 22:46:47.430 8612-8612/com.example.android.timer W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 22:46:47.430 8612-8612/com.example.android.timer W/System.err: at android.os.Looper.loop(Looper.java:137)
10-22 22:46:47.440 8612-8612/com.example.android.timer W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4940)
10-22 22:46:47.440 8612-8612/com.example.android.timer W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
10-22 22:46:47.440 8612-8612/com.example.android.timer W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
10-22 22:46:47.440 8612-8612/com.example.android.timer W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
10-22 22:46:47.440 8612-8612/com.example.android.timer W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
10-22 22:46:47.440 8612-8612/com.example.android.timer W/System.err: at dalvik.system.NativeStart.main(Native Method)
MainActivity.java:147 where the problem is located is the line 问题所在的MainActivity.java:147是该行
mediaPlayer.setDataSource(afd.getFileDescriptor());
My mp3 file is located at assets/sounds/alert.mp3 and I can see that in the project manager it is represented with a question mark. 我的mp3文件位于asset / sounds / alert.mp3,我可以在项目管理器中看到它以问号表示。 Any ideas? 有任何想法吗?
Try: 尝试:
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
If it doesn't work then you can add your mp3 to raw folder and then: 如果不起作用,则可以将mp3添加到原始文件夹,然后:
MediaPlayer player = MediaPlayer.create(this, R.raw.mp3_file);
Well the answer was simple eventually. 好吧,答案最终很简单。 Replaced 已更换
mediaPlayer.setDataSource(afd.getFileDescriptor());
with the following 与以下
mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
在res目录中制作一个Raw文件夹,并将所有mp3文件放在raw文件夹中,并像R.raw.alert.mp3这样引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.