6

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

dda
  • 6,030
  • 2
  • 25
  • 34
Priska Aprilia
  • 1,149
  • 1
  • 15
  • 34

5 Answers5

4

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();
dda
  • 6,030
  • 2
  • 25
  • 34
Priska Aprilia
  • 1,149
  • 1
  • 15
  • 34
  • 1
    thanks.. worked for me, but without afd.close(); working in Samsung my 10" ICS tab, but not in Nexus 7 Jellybean.. after adding afd.close(); worked in Jeallybean!! – LOG_TAG Nov 10 '12 at 12:18
3

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();
        }
Vipul
  • 27,808
  • 7
  • 60
  • 75
  • thanks for your reply. I have tried your suggestion, no more error(1,-4) but it gives me another error, the media player cannot be created. here is my logcat 06-16 23:33:53.350: W/System.err(14869): at android.media.MediaPlayer.setDataSource(Native Method) – Priska Aprilia Jun 16 '12 at 16:35
1

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

Community
  • 1
  • 1
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Actually I stored the audio file name in a database and the real audio files in assets folder. I use sqlite query to retrieve the file name from the database, and the audio in assets folder will be played once a specific audio file is clicked. Because I don't exactly know which audio is clicked, so I can't place the audio files beneath the res/raw folder. any idea? – Priska Aprilia Jun 16 '12 at 16:16
  • thanks for your reply. Yes that's what I have done but it gives me an error like I said before. – Priska Aprilia Jun 16 '12 at 16:23
1

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

DrA
  • 437
  • 2
  • 11
  • Yes I noticed that too I have converted my audio file to .wav format, but still same error "unable to create media player". Where do I have to place the player.setAudioStream(AudioManager.STREAM_MUSIC) line? edited : anyway there is setAudioSteamType function instead of setAudioStream. – Priska Aprilia Jun 17 '12 at 01:00
0

use this way it will solve your problem :)

    public void playBeep() {
    try {

        if (m.isPlaying()) {
            m.stop();
            m.release();
            m = new MediaPlayer();
        }
        AssetFileDescriptor descriptor = getAssets().openFd("mp3 name.mp3");
        m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
        descriptor.close();

        m.prepare();
        m.setVolume(1f, 1f);
        m.setLooping(true);
        m.start();
    } catch (Exception e) {
    }
}
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150