I need your help. I tried to play an audio file stored in Assets folder but an error occurred.
Here are my code:
try{
if (player.isPlaying()) {
player.stop();
player.release();
}
}catch(Exception e){
Toast.makeText(this, "an exception occurred", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
try{
AssetFileDescriptor afd = BeeDailyConvo.this.getAssets().openFd("sounds/hello_kr.wma");
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
}catch(Exception e){
e.printStackTrace();
}
And here are my logcat:
06-16 22:39:53.330: W/MediaPlayer(13490): info/warning (1, 26)
06-16 22:39:53.330: E/MediaPlayer(13490): error (1, -4)
Could you please explain what's wrong with my code?
Thank you in advance
Regards,
Priska
This issue has been SOLVED.
The asset file descriptor must be closed before preparing the player. This is how I solved the problem:
player = new MediaPlayer();
AssetFileDescriptor afd = BeeDailyConvo.this.getAssets()
.openFd("sounds/"+file);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
afd.close();**//just added this line**
player.prepare();
player.start();
Here you can see all Error codes Media player Error codes
-4 error code indicates you have given invalid arguments.
Put your code in try catch block.
Try Using
try {
AssetFileDescriptor afd = CustomListViewActivity.this.getAssets()
.openFd("sounds/hello_kr.wma");
player.setDataSource(afd.getFileDescriptor());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Unfortunately there is very little information about MediaPlayer error codes available for some reason. However I suggest you try putting your sound file inside res/raw/ instead of your assets.
EDIT:
Start here with the Using the MediaPlayer section in the developer docs. This will show you how to set up and play the sound properly.
EDIT 2:
turns out that can do it from assets see this question: Play audio file from the assets directory
I don't think that wma files are supported.
http://developer.android.com/guide/appendix/media-formats.html
I noticed that you didn't specify the audioStreamType
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MISIC);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With