2012-01-17 92 views
1

我有一個包含本地資源(音頻文件)的所有資源ID的數組。我試圖一個接一個播放所有這些文件。爲此,我在玩下一個文件的媒體播放器的setOnCompletionListener事件,如下圖所示: -播放音頻文件時出現異常

mMediaPlayer.setOnCompletionListener(); 

完整代碼連續播放文件是如下: -

代碼

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

    }); 
} 

如果我單獨播放所有文件,它們會完美播放。但使用上面的代碼,我在MediaPlayer中出錯。以下是堆棧跟蹤。

堆棧跟蹤

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) 

而且,相同的代碼完全運行在極少數的設備(但在大多數設備+模擬器),它提供了異常以上。

請爲此提供一些解決方案。

+1

我認爲這可能是一個對你的情況很好的答案:http://stackoverflow.com/questions/3667782/android-media-player-fails-mp3-with-pvmferrnotsupported – Zappescu 2012-01-17 16:33:37

+1

該問題應該來自於沒有準備() 方法。 http://developer.android.com/reference/android/media/MediaPlayer.html#prepare%28%29 – 2012-01-17 16:33:55

回答

0

你需要調用prepareAsync(),如:

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