Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Media player error (1,-4) while playing an audio from Assets folder

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

like image 356
Priska Aprilia Avatar asked Jun 16 '12 15:06

Priska Aprilia


4 Answers

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();
like image 121
Priska Aprilia Avatar answered Oct 04 '22 21:10

Priska Aprilia


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();
        }
like image 22
Vipul Avatar answered Oct 04 '22 19:10

Vipul


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

like image 32
FoamyGuy Avatar answered Oct 04 '22 20:10

FoamyGuy


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);

like image 32
DrA Avatar answered Oct 04 '22 20:10

DrA