Miko Diko
Miko Diko

Reputation: 954

Playing a video after a video in Android's TextureView has issues

Using a MediaPlayer inside a TextureView to play videos, it works.

When a video ends and another video should be played, I resize the TextureView to fit the up-coming video's aspect-ratio (example - from 3:4 to 9:16 and so on..).

The problem, after the first video ends, it's last frame remains shown, Than the TextureView is resized, the last frame of the first video remains for a split second but now with a bad aspect-ratio, the second video is being played well.

Images of videos showing a (Size X Size) rectangles:

Image1: video 1, (3:4):

enter image description here

Image2: video 1, here we are between the 2 videos, video 1's last frame, after resizing it's now at (9:16), but the rectangle is looking bad. the below picture is shown for a split second:

enter image description here

Image3: video 2, (9:16), playing well but after showing image2 for a split second:

enter image description here

There's nothing special about the code being used, here are some parts of it:

// To init the TextureView and MediaPlayer:

public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
    m_MediaPlayer = new MediaPlayer();
    m_Surface = new Surface(surfaceTexture);
    m_MediaPlayer.setSurface(m_Surface);
}

m_TextureView.setSurfaceTextureListener(this);

// To Resize:
LayoutParams layoutParams = new FrameLayout.LayoutParams(newTVWidth, newTVHeight);
layoutParams.gravity = Gravity.CENTER;
m_TextureView.setLayoutParams(layoutParams);

// To play:
m_MediaPlayer.reset();
m_MediaPlayer.setDataSource(videoFilePath);
m_MediaPlayer.prepare();
// <--- Here comes a call to "To Resize" which is shown right above this code
m_MediaPlayer.start();

Upvotes: 5

Views: 4693

Answers (1)

fadden
fadden

Reputation: 52313

You have a couple of options, maybe.

In theory you can clear the Surface by disconnecting the media player, clearing the surface with GLES, and then connecting the media player again. See clickPlayStop() and clearSurface() in Grafika's PlayMovieSurfaceActivity. This uses MediaCodec, not MediaPlayer, so I'm not sure how it'll translate.

Another option is to use the TextureView transform rather than a custom frame layout to set your aspect ratio. Because your layout doesn't change, there is no flash of a distorted frame. See adjustAspectRatio() in PlayMovieActivity for an example.

Upvotes: 2

Related Questions