Reputation: 121
I am trying to make my app use an alert sound through the following code but i get an 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
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. Any ideas?
Upvotes: 0
Views: 1608
Reputation: 841
Make A raw folder in res directory and put all mp3 files in raw folder and reference like this R.raw.alert.mp3.Sure this help thanks
Upvotes: 2
Reputation: 121
Well the answer was simple eventually. Replaced
mediaPlayer.setDataSource(afd.getFileDescriptor());
with the following
mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
Upvotes: 0
Reputation: 951
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:
MediaPlayer player = MediaPlayer.create(this, R.raw.mp3_file);
Upvotes: 2