Playback video in slow motion in android

2020-02-26 11:56发布

问题:

- I am working on a project which needs to play video in slow motion.

- I am well aware that Android doesn't provide these functionality.

- I found PVPlayer Engine and libVLC which possessed these capabilities, but i didn't found any tutorial or proper documentation of including them in the android project and using them.

- So i tried doing this by using Runnable and Handler, it was successful in slowing down the video but they possessed jerks during playing.

public class MainActivity extends Activity {

    VideoView vx;
    Button mbutt;
    Handler h ;
    int curr = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        h = new Handler();

        vx = (VideoView)findViewById(R.id.videoView);
        mbutt = (Button)findViewById(R.id.button_Play);

        vx.setVideoPath("/mnt/sdcard/you.mp4");

        mbutt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                vx.start();
            }
        });


        Runnable r = new Runnable() {

            @Override
            public void run() {

                if (vx != null) {

                    if (vx.isPlaying()){

                        vx.pause();  
                    }
                    else{                        
                        vx.start(); 
                    }
                }

                h.postDelayed(this, 50);
            }
        };

        h.postDelayed(r, 200);






    }


}

- I have tried various combination of pause time and playing time to remove the jerks but all in vain, can anyone help me in removing these jerks so it plays a nice slow motion video or suggest another easy to integrate library into my android project.

Thanks in advance......

回答1:

I am late but I found a solution for API 23 and above. Android 6.0 added PlaybackParams class to control playback behavior. -

Use setPlaybackParams method of MediaPlayer as given below -

videoview = (VideoView)findViewById(R.id.videoview);
videoview.setVideoURI("Your Video URI"); 
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    //works only from api 23
                    PlaybackParams myPlayBackParams = new PlaybackParams();
                    myPlayBackParams.setSpeed(0.5f); //here set speed eg. 0.5 for slow 2 for fast mode
                    mp.setPlaybackParams(myPlayBackParams);

                    videoview.start();//start your video.
                }
        });


回答2:

If you are searching how to embedded VLC into android, you can ref to this. and you can change the speed by calling setRate(0.5f) to libVLC for slow motion.