ExoPlayer icon indicating copy to clipboard operation
ExoPlayer copied to clipboard

Dealing with the shutter

Open ChristianKleineidam opened this issue 8 years ago • 9 comments

ExoPlayer2 frequently shows a black shutter for a few milliseconds while the default media player doesn't. Given that the shutter only shows for a few milliseconds it's not easy to know exactly when it appears.

I think it would be great if there would be a function at ExoPlayer.EventListener whenever the shutter gets activated or disactivated.

If I understand the current behavior right, when I create a new Fragment instance that creates a MediaPlayer in onCreate(), the view of the fragment gets only rendered once the MediaPlayer is actually ready to play the file. On the other hand, it seems like ExoPlayer2 does all the preparation asynchronously and as a result the view gets rendered before the player is ready to play the file. This then results in the need to display something before the video is ready and this is one instance where the shutter gets actually displayed. I think the MediaPlayer behavior makes much more sense and provides for a better user experience than flashing shortly the black screen. Is there a way to get the same behavior with ExoPlayer2?

ChristianKleineidam avatar May 27 '18 10:05 ChristianKleineidam

If I understand the current behavior right, when I create a new Fragment instance that creates a MediaPlayer in onCreate(), the view of the fragment gets only rendered once the MediaPlayer is actually ready to play the file. On the other hand, it seems like ExoPlayer2 does all the preparation asynchronously and as a result the view gets rendered before the player is ready to play the file. This then results in the need to display something before the video is ready and this is one instance where the shutter gets actually displayed. I think the MediaPlayer behavior makes much more sense and provides for a better user experience than flashing shortly the black screen. Is there a way to get the same behavior with ExoPlayer2?

Is your use case playing only local files, and are you using the synchronous MediaPlayer.prepare from the UI thread? That's the only use case for which I think these comments make sense, since for all other cases you should be using MediaPlayer.prepareAsync or calling MediaPlayer.prepare on a background thread.

I don't think there's a way to block the UI thread whilst ExoPlayer prepares, which is effectively what you're trying to do. I wonder whether it works to reverse the order in which you're doing things. In other words, rather than create the Fragment and then create the player, could you instead create the player and then create the Fragment once the player has rendered the first frame (which should happen very quickly for local files)?

If that doesn't work, it might be possible for us to add a blocking implementation of prepare, although I'm not particularly keen on the idea. We've gone multiple years and this is the first request we've had for it, which suggests the vast majority of developers do not need it.

ojw28 avatar May 29 '18 16:05 ojw28

I think it would be great if there would be a function at ExoPlayer.EventListener whenever the shutter gets activated or disactivated.

I think we already have this. Register a video listener (SimpleExoPlayer.addVideoListener) and implement onRenderedFirstFrame, which is called when the first frame is rendered and the shutter is hidden.

ojw28 avatar May 29 '18 16:05 ojw28

Yes, I'm playing local files. I use ExoPlayer because of it's capabilities for controlling the playback speed.

Till now it was my understanding that ExoPlayer needs to be attached to a view to know the size of video in which it's rendered. If that's not the case, for my use case it sounds like a good solution to prepare the ExoPlayer beforehand and only create the Fragment when it's ready.

If I understand you correctly then I have to setPlayWhenReady(false) and then wait for onPlayerStateChanged with Player.READY and when I then create my fragment it should play without showing any black screen?

ChristianKleineidam avatar May 29 '18 22:05 ChristianKleineidam

I now changed my code to only create the fragment once the player is ready but it still flashes the black screen before playing the video.

As far as the listener goes, that listener doesn't tell me when the shutter starts getting shown.

ChristianKleineidam avatar May 30 '18 20:05 ChristianKleineidam

When I try to log what I can log, I get:

05-30 23:20:50.363 31214-31214/com.LearningLeaflets.BetterEar.English V/OurPlayer: PlayerStateChanged to 3 and playWhenReady true
05-30 23:20:50.383 31214-32423/com.LearningLeaflets.BetterEar.English I/MediaCodec: [OMX.qcom.video.decoder.avc] setting surface generation to 31963186
05-30 23:20:50.383 31214-32424/com.LearningLeaflets.BetterEar.English D/SurfaceUtils: set up nativeWindow 0x7f9e546a10 for 800x720, color 0x7fa30c06, rotation 0, usage 0x2002900
05-30 23:20:50.393 31214-31214/com.LearningLeaflets.BetterEar.English V/VIDEO_TABLE: VideoFragment gets created
05-30 23:20:50.423 31214-32423/com.LearningLeaflets.BetterEar.English I/MediaCodec: [OMX.qcom.video.decoder.avc] setting surface generation to 31963187
05-30 23:20:50.423 31214-32424/com.LearningLeaflets.BetterEar.English D/SurfaceUtils: set up nativeWindow 0x7f9e547810 for 800x720, color 0x7fa30c06, rotation 0, usage 0x2002900
05-30 23:20:50.443 31214-31214/com.LearningLeaflets.BetterEar.English D/OurPlayer: onVideoSizeChanged to width:800 and height:720
05-30 23:20:50.443 31214-31214/com.LearningLeaflets.BetterEar.English W/OurPlayer: onRenderedFirstFrame

Is the "native video" the black screen? (OurPlayer is my class that manages the SimpleExoPlayer)

ChristianKleineidam avatar May 30 '18 21:05 ChristianKleineidam

I did create a minimal example to show that there's still a black screen showing when a new Fragment gets displayed: https://github.com/ChristianKleineidam/VideoProblems

ChristianKleineidam avatar May 31 '18 15:05 ChristianKleineidam

The surface prior to anything having been drawn into it is the back screen. On further reflection it seems to me there's a bit of a chicken and egg problem: You normally need to make the SurfaceView/TextureView visible for it to create the underlying surface that you draw into, but you want to not make the view visible until after the surface contains the first frame. I'm not exactly sure how the MediaPlayer case avoids this issue, either. We'll have to do some digging.

ojw28 avatar Jun 01 '18 14:06 ojw28

hi @ChristianKleineidam, did you get any further with this? Having the exact same issue myself!

ntohill95 avatar Feb 13 '19 16:02 ntohill95

Hi @ChristianKleineidam - Sorry for the very delayed response, but I had a bit of time to play around with this. It looks like you can make the SurfaceView transparent until the first frame is rendered into it, like this:

SurfaceView surfaceView = view.findViewById(R.id.surface_view1);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.setFormat(PixelFormat.TRANSPARENT);

I was able to edit VideoFragment in your demo app to do this, and it removed the black flicker. I got this from https://stackoverflow.com/questions/5391089/how-to-make-surfaceview-transparent.

I will close this because I think this is probably what you're looking for. It's also quite possible that you're no longer interested, due to the elapsed time! If you are still interested and have further questions, please respond here and we can re-open the issue.

ojw28 avatar Mar 12 '20 18:03 ojw28