1

I have an array with all resource id of local resource (audio files). I am trying to play all these file one by one. For this purpose, i am playing next file on the setOnCompletionListener event of Media Player, as below:-

mMediaPlayer.setOnCompletionListener();

Complete code for continuously playing files is as below:-

Code

private void playing(){
  MediaPlayer mMediaPlayer = new MediaPlayer();
  mMediaPlayer = MediaPlayer.create(this, Constants.mAudioIds[play]);  // next file will be played
  mMediaPlayer.start();
  mMediaPlayer.setOnCompletionListener(new OnCompletionListener(){
      @Override
     public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
        play +=1;
        playing();
     }

    });
}

If i play all files individually, they are playing perfectly. But using above code, i m getting error in MediaPlayer. Below is the stack trace.

StackTrace

01-17 20:52:10.088: INFO/MediaPlayer(379): Info (1,44)
01-17 20:52:10.358: DEBUG/dalvikvm(215): GC freed 43 objects / 2096 bytes in 108ms
01-17 20:52:15.368: WARN/MediaPlayer(379): info/warning (1, 26)
01-17 20:52:15.410: ERROR/PlayerDriver(31): Command PLAYER_PREPARE completed with an error or info PVMFErrResource
01-17 20:52:15.419: ERROR/MediaPlayer(379): error (1, -17)
01-17 20:52:15.462: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete
01-17 20:52:15.568: DEBUG/MediaPlayer(379): create failed:
01-17 20:52:15.568: DEBUG/MediaPlayer(379): java.io.IOException: Prepare failed.: status=0x1
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at android.media.MediaPlayer.prepare(Native Method)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at android.media.MediaPlayer.create(MediaPlayer.java:644)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at com.media.sample.ui.AutoPlay.playing(AutoPlay.java:162)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at com.media.sample.ui.AutoPlay.access$0(AutoPlay.java:138)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at com.media.sample.ui.AutoPlay$1.onCompletion(AutoPlay.java:171)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1157)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at android.os.Looper.loop(Looper.java:123)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at android.app.ActivityThread.main(ActivityThread.java:4363)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at java.lang.reflect.Method.invokeNative(Native Method)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at java.lang.reflect.Method.invoke(Method.java:521)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-17 20:52:15.568: DEBUG/MediaPlayer(379):     at dalvik.system.NativeStart.main(Native Method)

Also, Same code runs perfectly in very few devices (but in most of devices + emulator) it gives above exception.

Please provide some solution for the same.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Sulabh Gupta
  • 642
  • 1
  • 7
  • 20
  • 1
    I think this could be an answer good for your case: http://stackoverflow.com/questions/3667782/android-media-player-fails-mp3-with-pvmferrnotsupported – Zappescu Jan 17 '12 at 16:33
  • 1
    The problem should come from the absence of prepare() method. http://developer.android.com/reference/android/media/MediaPlayer.html#prepare%28%29 – Jeremy D Jan 17 '12 at 16:33

2 Answers2

0

You need to consult the state diagram and chart that outlines the states that the mediaplayer can be in and which methods can be called in which state. http://developer.android.com/reference/android/media/MediaPlayer.html#Valid_and_Invalid_States

The mediaplayer can be a bit tricky because of this.

LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
0

You need to call prepareAsync() like:

private void playing(){
  MediaPlayer mMediaPlayer = new MediaPlayer();
  mMediaPlayer = MediaPlayer.create(this, Constants.mAudioIds[play]);  
  mMediaPlayer.prepareAsync();

  mMediaPlayer.setOnCompletionListener(new OnCompletionListener(){
      @Override
     public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
        play +=1;
        playing();
     }

    });
}

 @Override
 public void onPrepared(MediaPlayer mp) {
  Log.d(TAG, "Stream is prepared");
  mp.start();
 }

Complete example is here.

Community
  • 1
  • 1
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149