0

my mediaplayer skips forward some seconds when the surfaceview gets destroyed. It is a video player consisting out of 2 fragments. in each fragment is a surfaceview where the video should be seen. and if I switch between the 2 fragments I have to call 'mPlayer.setDisplay(surfaceHolder);'. Otherwise the video won't be shown. but there is one problem the player skips forward some seconds. The reason is that the video buffer gets dumped when the method surfaceDestroyed gets called. according to this

I already tried out a little bit with surfacetexture and textureview. Therefore I declared a static SurfaceTexture variable in my mainActivity where I hold my surfaceTexture when I switch between the fragments. after a switch I set my surfacetexture to the textureview.

textureView.setSurfaceTexture(MainActivity.getSurfaceTexture());

This even works when I switch for the first time without skipping forward or interruption. but then when I switch the second time a runtimeexception gets thrown..

attachToContext: GLConsumer is already attached to a context     
Shutting down VM   
W/dalvikvm(8400): threadid=1: thread exiting with uncaught exception (group=0xa4d25b20)   
FATAL EXCEPTION: main   
Process: info.androidhive.slidingmenu, PID: 8400   
java.lang.RuntimeException: Error during attachToGLContext (see logcat for details)   
at android.graphics.SurfaceTexture.attachToGLContext(SurfaceTexture.java:215)   
at android.view.GLES20TextureLayer.setSurfaceTexture(GLES20TextureLayer.java:86)    
at android.view.HardwareRenderer$Gl20Renderer.setSurfaceTexture(HardwareRenderer.java:2221)
at android.view.TextureView.getHardwareLayer(TextureView.java:401)   
at android.view.View.getDisplayList(View.java:13328)   
at android.view.View.getDisplayList(View.java:13404)   
at android.view.View.draw(View.java:14182)   
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)   
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)   
at android.view.View.getDisplayList(View.java:13357)   
at android.view.View.getDisplayList(View.java:13404)   
at android.view.View.draw(View.java:14182)   
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)   
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)   
at android.view.View.draw(View.java:14468)   
at android.view.View.getDisplayList(View.java:13362)   
at android.view.View.getDisplayList(View.java:13404)   
at android.view.View.draw(View.java:14182)   
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)   
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)    
at android.view.View.getDisplayList(View.java:13357)   
at android.view.View.getDisplayList(View.java:13404)   
at android.view.View.draw(View.java:14182)   
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)   
at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:804)   
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)   
at android.view.View.getDisplayList(View.java:13357)    
at android.view.View.getDisplayList(View.java:13404)   
at android.view.View.draw(View.java:14182)   
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)   
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)   
at android.view.View.getDisplayList(View.java:13357)    
at android.view.View.getDisplayList(View.java:13404)   
at android.view.View.draw(View.java:14182)   
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)   
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)   
at android.view.View.draw(View.java:14468)    
at com.android.internal.widget.ActionBarOverlayLayout.draw(ActionBarOverlayLayout.java:381)
at android.view.View.getDisplayList(View.java:13362)   
at android.view.View.getDisplayList(View.java:13404)   
at android.view.View.draw(View.java:14182)   
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)   
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)   
at android.view.View.draw(View.java:14468)   
at android.widget.FrameLayout.draw(FrameLayout.java:472)   
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2326)   
at android.view.View.getDisplayList(View.java:13362)   
at android.view.View.getDisplayList(View.java:13404)   
at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1570)   
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1449)   
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2377)    
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2249)     
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1879)    
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)   
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)   
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)    
at android.view.Choreographer.doCallbacks(Choreographer.java:574)    
at android.view.Choreographer.doFrame(Choreographer.java:544)    
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)   
at android.os.Handler.handleCallback(Handler.java:733)   
at android.os.Handler.dispatchMessage(Handler.java:95)   
at android.os.Looper.loop(Looper.java:136)   
at android.app.ActivityThread.main(ActivityThread.java:5001)   
at java.lang.reflect.Method.invokeNative(Native Method)   
at java.lang.reflect.Method.invoke(Method.java:515)   
at com. 

Does anybody know why this exception gets called?

Community
  • 1
  • 1
mario
  • 31
  • 4

1 Answers1

0

This happens when you try to attach to a texture that still is attached to a different view. You have to wait for the view to detach. This will happen in the onDetachedFromWindow() call. I know it's not ideal (we've been battling through synchronization issues between views that share the surface), but that's the only thing I can really think of unless Android decides to make this situation non-fatal.

You could consider wrapping the reference to your SurfaceTexture in a class that keeps track of it it's attached or not (using your calls to setSurfaceTexture() and onDetachedFromWindow() and only adding your second view to the hierarchy once the wrapped texture knows that it's detached.

natez0r
  • 1,064
  • 9
  • 14