媒体播放器抛出异常

时间:2011-12-11 06:04:31

标签: android streaming media-player

我正在开发Android媒体播放器流媒体应用程序。我得到以下异常:

 12-11 11:12:06.298: INFO/StagefrightPlayer(34): setDataSource('10.0.2.2\a.mp3')
 12-11 11:12:06.308: DEBUG/mp(618): starting
 12-11 11:12:06.308: ERROR/MediaPlayer(618): start called in state 4
 12-11 11:12:06.308: ERROR/MediaPlayer(618): error (-38, 0)
 12-11 11:12:06.328: ERROR/MediaPlayer(618): error (1, -2147483648)
 12-11 11:12:06.328: ERROR/MediaPlayer(618): Error (-38,0)
 12-11 11:12:06.409: ERROR/MediaPlayer(618): Error (1,-2147483648)
        String url = "10.0.2.2\\a.mp3"; // your URL here
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(url);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            mediaPlayer.prepareAsync();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.d("mp","starting");
        mediaPlayer.start();

使用mediaPlayer.prepare时收到以下错误消息:

 12-11 12:08:42.367: ERROR/MediaPlayer(980): error (1, -2147483648)
 12-11 12:08:42.367: WARN/System.err(980): java.io.IOException: Prepare failed.: status=0x1
 12-11 12:08:42.378: WARN/System.err(980):     at android.media.MediaPlayer.prepare(Native Method)
 12-11 12:08:42.378: WARN/System.err(980):     at mobiledo.pjct.Voicesms.onCreate(Voicesms.java:52)
 12-11 12:08:42.387: WARN/System.err(980):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 12-11 12:08:42.387: WARN/System.err(980):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
 12-11 12:08:42.387: WARN/System.err(980):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 12-11 12:08:42.387: WARN/System.err(980):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 12-11 12:08:42.387: WARN/System.err(980):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 12-11 12:08:42.387: WARN/System.err(980): at android.os.Handler.dispatchMessage(Handler.java:99)
 12-11 12:08:42.418: WARN/System.err(980):     at android.os.Looper.loop(Looper.java:123)
 12-11 12:08:42.418: WARN/System.err(980):     at android.app.ActivityThread.main(ActivityThread.java:3683)
 12-11 12:08:42.418: WARN/System.err(980):     at java.lang.reflect.Method.invokeNative(Native Method)
 12-11 12:08:42.418: WARN/System.err(980):     at java.lang.reflect.Method.invoke(Method.java:507)
 12-11 12:08:42.418: WARN/System.err(980):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 12-11 12:08:42.418: WARN/System.err(980):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 12-11 12:08:42.418: WARN/System.err(980):     at dalvik.system.NativeStart.main(Native Method)             

2 个答案:

答案 0 :(得分:3)

您异步准备MediaPlayer实例,但在此之后立即调用start,这会导致异常,因为MediaPlayer尚未就绪。您应该注册OnPreparedListener(在致电prepareAsync之前)并在此听众致电start()中注册:

mediaPlayer.setOnPreparedListener(new OnPreparedListener(){
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }
});
mediaPlayer.prepareAsync();

答案 1 :(得分:1)

调用.prepareAsync()会将播放器移动到准备状态,而不是准备状态。因此,请改为调用.prepare,或者实施并注册OnPreparedListener,然后调用start()