I'm working on a multimedia video processing application for Android, and I've run into a bit of a problem. I'm using the FragmentPagerAdapter
class with a number of different fragments inside for various steps of video processing.
My first Fragment
contains a SurfaceView
and a MediaPlayer
that feeds it, along with various playback controls. My problem happens when I swipe from fragment 1 (with the SurfaceView) to fragment 2 (empty at the moment). If I haven't yet called start()
, nothing unusual happens, and I'm able to swipe between fragments normally. Once I call start()
however, the entire screen starts flashing on and off when I swipe to the next Fragment
, even if I've put the MediaPlayer
in the idle state and it's not feeding frames to SurfaceView
. The only thing that stops this is destroying the surface along with the containing view by swiping to the third Fragment
so the FragmentPagerAdapter
destroys Fragment
one, or exiting the application via the home or back button so the view is destroyed.
I can't for the life of me figure out why this is happening, other than that perhaps the SurfaceView
rendering thread is somehow interfering with the main UI thread. Nothing unusual appears on LogCat, either, so I'm a bit stuck. I'm running a Galaxy Nexus with android 4.1 as my test hardware.
Any help would be appreciated!
JT
UPDATE: I've managed to find a workaround for now by overriding the setPrimaryItem()
method in the FragmentPagerAdapter
to call a method that removes the SurfaceView
from the hierarchy (using removeView()
on its LinearLayout
container) when the video player Fragment
ceases to be displayed, and then reinstates the SurfaceView
when it's active again. There's still a bit of a blink when this happens, unfortunately, so if anyone has additional thoughts, I'd be grateful!
From the Android Developers Blog
This widget[SurfaceView] works by creating a new window placed behind your application’s window. It then punches a hole through your application’s window to reveal the new window.
Because a SurfaceView’s content does not live in the application’s window, it cannot be transformed (moved, scaled, rotated) efficiently. This makes it difficult to use a SurfaceView inside a ListView or a ScrollView.
The Solution is to use a TextureView if you're building for 4.0 or above. If youre interested in using a TextureView to display video this thread might be helpful
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With