2017-06-24 98 views
-2

當我調用Mediaplayer時,在某些較新的Android模型上發生IllegalStateException。它主要是Android 6.和7手機。我似乎無法弄清楚如何解決這個問題,而這裏的其他「問題」似乎並不適用於我的問題。致命異常:android.media.MediaPlayer上的java.lang.IllegalStateException

正在此堆棧跟蹤:

Fatal Exception: java.lang.IllegalStateException 
    at android.media.MediaPlayer._prepare(MediaPlayer.java) 
    at android.media.MediaPlayer.prepare(MediaPlayer.java:1369) 
    at org.my.app.IntroVideoSurfaceView.surfaceCreated(IntroVideoSurfaceView.java:51) 
    at android.view.SurfaceView.updateWindow(SurfaceView.java:712) 
    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:316) 
    at android.view.View.dispatchWindowVisibilityChanged(View.java:10434) 
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1330) 
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1330) 
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1330) 
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1330) 
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751) 
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1438) 
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7398) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:695) 
    at android.view.Choreographer.doFrame(Choreographer.java:631) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:158) 
    at android.app.ActivityThread.main(ActivityThread.java:7224) 
    at java.lang.reflect.Method.invoke(Method.java) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

這是我的課:

public class IntroVideoSurfaceView extends SurfaceView implements SurfaceHolder.Callback { 

private MediaPlayer mp; 
private boolean has_started = false; 

@TargetApi(Build.VERSION_CODES.LOLLIPOP) public IntroVideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
    init(); 
} 

public IntroVideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(); 
} 

public IntroVideoSurfaceView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
} 

public IntroVideoSurfaceView(Context context) { 
    super(context); 
    init(); 
} 

private void init() { 
    mp = new MediaPlayer(); 
    getHolder().addCallback(this); 
} 

@Override public void surfaceCreated(SurfaceHolder holder) { 
    AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.onboarding); 
    try { 
     if (!has_started) { 
      has_started = true; 
      mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength()); 
     } 


     mp.prepare(); 


     android.view.ViewGroup.LayoutParams lp = getLayoutParams(); 
     lp.height = getHeight(); 
     lp.width = getWidth(); 

     setLayoutParams(lp); 
     mp.setDisplay(getHolder()); 
     mp.setLooping(true); 
     mp.start(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
} 

@Override public void surfaceDestroyed(SurfaceHolder holder) { 
    mp.stop(); 
    } 
} 

這是我的xml文件我在活動中呼籲,以顯示視頻:

 <org.my.app.IntroVideoSurfaceView 
     android:id="@+id/surface" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
+0

哪一行是51號? – Sanoop

+0

Line numer 51是:mp.prepare(); – datasmurfen

+2

你確定當你到達mp.prepare()mp狀態「初始化」 – Alex

回答

1

在循環結束時,你的媒體播放器start()Started狀態)。如果你調用prepare()後來那麼IllegalStateException將拋出,因爲你只能從Initialized狀態調用prepare()(你把媒體播放器與surfaceDestroyed()Stopped狀態不是有效狀態來調用prepare())。請參閱state diagram here,並注意根據the surface life-cyclesurfaceCreated()可能會被多次調用。因此,您至少應該在if區塊內放置mp.prepare()

相關問題