0

I have an app with a news feed in which there can be multiple content types. I am using a RecyclerView for the list.

I have a problem where if there is a video playing in the feed and they user sends the app to the background and then relaunches the app from the recent list, press play again, the video is the wrong size.

I've attached a couple of images to illustrate the problem:

This is when the app is launched and a video is played normally

First time play before being sent to background

This is after being sent to the background and then launched from the recent list. (Video is manually started as per requirements)

After onResume 1st time

This is after sending app to background for second time and relaunching from recent list.

After onResume 2nd Time

So when the app is sent to the background onSurfaceTextureDestroyed is called.

 @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {

        surface.release();
        L.i("SURFACE TEXTURE ", "DESTROYED: " + String.valueOf(surface));
        if(!mFullScreen) {
            if(mMediaPlayer!=null) {
                mCurrentPosition = mMediaPlayer.getCurrentPosition();
                releaseMediaPlayer();
            }
        }

So I call release media player here.

  //Stop Media player and release resources
    public void releaseMediaPlayer(){
        if(mMediaPlayer!=null){
            if(mMediaPlayer.isPlaying()){
                mMediaPlayer.stop();
            }
            mCurrentPosition = mMediaPlayer.getCurrentPosition();
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

My method for playing my videos is as follows:

 public void startVideo(String url) {
        mUrl = url;

        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = new MediaPlayer();
        }
        else {
            mMediaPlayer = new MediaPlayer();
        }

        try {
            L.i(getClass().getName(), mUrl);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnInfoListener(this);
            mMediaPlayer.setOnErrorListener(this);
            mMediaPlayer.setSurface(new Surface(mSurface));
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(url);
            mMediaPlayer.prepareAsync();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

and then in my onPrepared method

@Override
public void onPrepared(MediaPlayer mp) {

    mTextureView.setOnTouchListener(new ControlTouchListener());
    mControllerView.setMediaPlayer(this);
    mControllerView.setAnchorView(mAnchorView);

    L.i(getClass().getSimpleName(), "Video Height: " + String.valueOf(mMediaPlayer.getVideoHeight()) + "Video Width: " + String.valueOf(mMediaPlayer.getVideoWidth()));
    mMediaPlayer.getVideoHeight();

    if (mMimeType != null && mMimeType.contains("audio")) {
        mProgress.setVisibility(View.GONE);
        mControllerView.setTimeout(mMediaPlayer.getDuration());
        mControllerView.getFullScreenButton().setVisibility(View.GONE);
    }
    else {
        mControllerView.setTimeout(3000);
    }

    if (!mFullScreen) {
        if (mMimeType.contains("video")) {
            MediaUtils.handleVideoAspectRatio(mp, mTextureView, mControllerView);
        }
        else {
            mMediaPlayer.start();
            mControllerView.updatePausePlay();
        }
    }
    else {
        mMediaPlayer.start();
        mControllerView.updatePausePlay();
    }

}

I have an handleVideoAspectRatio method there also which is to ensure if the video is in portrait:

   public static void handleVideoAspectRatio(MediaPlayer mediaPlayer, TextureView textureView, MediaControllerView controllerView) {

        int textureViewHeight = textureView.getHeight();

        L.i("HANDLE ASPECT RATIO: " ,  "TEXTURE VIEW HEIGHT: "+String.valueOf(textureViewHeight));
        int textureViewWidth = textureView.getWidth();

        L.i("HANDLE ASPECT RATIO: " ,  "TEXTURE VIEW WIDTH: "+String.valueOf(textureViewWidth));

        float videoHeight = mediaPlayer.getVideoHeight();
        float videoWidth = mediaPlayer.getVideoWidth();

        float ratioHeight = textureViewHeight/videoHeight;
        float ratioWidth = textureViewWidth/videoWidth;

        float aspectRatio = videoWidth/videoHeight;

        ViewGroup.LayoutParams textureViewLayoutParams = textureView.getLayoutParams();

        if(ratioWidth>ratioHeight){
            textureViewLayoutParams.width = (int)(textureViewHeight * aspectRatio);
            textureViewLayoutParams.height = textureViewHeight;
        }else{
            textureViewLayoutParams.width = textureViewWidth;
            textureViewLayoutParams.height = (int)(textureViewWidth * aspectRatio);
        }

        textureView.setLayoutParams(textureViewLayoutParams);

        mediaPlayer.start();

        controllerView.updatePausePlay();
    }

I have tried to reset the size of the texture view with layout params before being played in case the recyclerview was holding onto parameters from a previous video but that doesn't work

Can anyone help and tell me why this is happening?

DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
  • 1
    I think your problem is your method handleVideoAspectRatio, is this method called every time you land on the screen? If it is then please check height and width you getting for your video. – Silvans Solanki Feb 29 '16 at 13:48
  • @SilvansSolanki yeah you're right... I had thought that, so that's why I was trying to resize the texture view and start again, since posting I found a simple boolean to check if the aspect ratio was handled or not was sufficient. Thanks for the comment – DJ-DOO Feb 29 '16 at 14:07
  • Good to know you have achieved your goal :) – Silvans Solanki Feb 29 '16 at 14:08

0 Answers0