1

My requirement is to change video inside video player with animation. When a person taps on "change" button the video in media player will get change with a new one and the old will end up with zoom in animation. I am able to implement this functionality with the help of texture view but I am getting one issue that there is a glitch while changing the video. is there any way I can make it more smooth?

Here is my code :

    public class VideoVieww extends Activity implements TextureView.SurfaceTextureListener  {

    private TextureView textureView;
    private MediaPlayer mMediaPlayer;
    private Button mButton;
    private Animation zoomAnimation;
    private SurfaceTexture mSurfaceTexture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.texture_layout);

        textureView = findViewById(R.id.textureView);
        textureView.setSurfaceTextureListener(this);
        zoomAnimation = AnimationUtils.loadAnimation(VideoVieww.this, R.anim.zomm_out);

        mButton =  findViewById(R.id.change);
        mButton.setOnClickListener(view -> {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
                mMediaPlayer.release();
                mMediaPlayer = null;
            }
            textureView.startAnimation(zoomAnimation);
                try {
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource(VideoVieww.this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.broadchurch));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    Surface surface = new Surface(mSurfaceTexture);
                    mMediaPlayer.setSurface(surface);
                    mMediaPlayer.prepare();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        });
        zoomAnimation.setAnimationListener(new Animation.AnimationListener(){
            @Override
            public void onAnimationStart(Animation arg0) {
            }
            @Override
            public void onAnimationRepeat(Animation arg0) {
            }
            @Override
            public void onAnimationEnd(Animation arg0) {
                mMediaPlayer.start();
            }


        });
    }



    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mMediaPlayer.release();
        mMediaPlayer=null;
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
        mSurfaceTexture=surfaceTexture;
        Surface surface = new Surface(surfaceTexture);
        try {
            mMediaPlayer= new MediaPlayer();
            mMediaPlayer.setDataSource(VideoVieww.this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sid));
            mMediaPlayer.setSurface(surface);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
//            mMediaPlayer.setLooping(true);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }
}

Any help would be greatly appreciated!!!

user2028
  • 163
  • 4
  • 15
  • 40
  • When do you see a glitch? Before starting animation or before starting next video? – maja89 Apr 26 '19 at 10:29
  • before starting the next video – user2028 Apr 26 '19 at 10:42
  • Try setting MediaPlayer when animation starts and when it ends just call mMediaPlayer.start(). – maja89 Apr 26 '19 at 12:26
  • To make sure you understood me. Do all the work you can upfront. So since you have two videos, one that is ending and the next that is starting, use two MediaPlayers. Prepare next one as soon as you can to avoid any possible delays. – maja89 Apr 26 '19 at 12:51
  • I tried with two media players but still, it is not smooth. – user2028 Apr 29 '19 at 05:42

0 Answers0