How to take a screenshot of Android's VideoVie

2019-05-04 23:57发布

问题:

v = new VideoView(this){
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(vWi,vHe);
    }
};

v.setVideoURI(Uri.parse("rtsp://xxxxxxxxx"));
v.requestFocus();
v.start();

How to take a screenshot? I try take screenshot, but picture is black.

回答1:

This is how it goes to capture screenshot of current frame in videoview

public class MainActivity extends Activity {

MediaMetadataRetriever mediaMetadataRetriever;
MediaController myMediaController;
VideoView myVideoView;
String viewSource = "/storage/sdcard0/DCIM/100MEDIA/VIDEO0009.mp4";

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

    mediaMetadataRetriever = new MediaMetadataRetriever();
    mediaMetadataRetriever.setDataSource(viewSource);

    myVideoView = (VideoView) findViewById(R.id.videoview);
    myVideoView.setVideoURI(Uri.parse(viewSource));
    myMediaController = new MediaController(this);
    myVideoView.setMediaController(myMediaController);

    myVideoView.setOnCompletionListener(myVideoViewCompletionListener);
    myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);
    myVideoView.setOnErrorListener(myVideoViewErrorListener);

    myVideoView.requestFocus();
    myVideoView.start();

    Button buttonCapture = (Button) findViewById(R.id.capture);
    buttonCapture.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            int currentPosition = myVideoView.getCurrentPosition(); // in
                                                                    // millisecond
            Toast.makeText(MainActivity.this,
                    "Current Position: " + currentPosition + " (ms)",
                    Toast.LENGTH_LONG).show();

            Bitmap bmFrame = mediaMetadataRetriever
                    .getFrameAtTime(currentPosition * 1000); // unit in
                                                                // microsecond

            if (bmFrame == null) {
                Toast.makeText(MainActivity.this, "bmFrame == null!",
                        Toast.LENGTH_LONG).show();
            } else {
                AlertDialog.Builder myCaptureDialog = new AlertDialog.Builder(
                        MainActivity.this);
                ImageView capturedImageView = new ImageView(
                        MainActivity.this);
                capturedImageView.setImageBitmap(bmFrame);
                LayoutParams capturedImageViewLayoutParams = new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
                capturedImageView
                        .setLayoutParams(capturedImageViewLayoutParams);

                myCaptureDialog.setView(capturedImageView);
                myCaptureDialog.show();
            }

        }
    });
}

MediaPlayer.OnCompletionListener myVideoViewCompletionListener = new MediaPlayer.OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer arg0) {
        Toast.makeText(MainActivity.this, "End of Video", Toast.LENGTH_LONG)
                .show();
    }
};

MediaPlayer.OnPreparedListener MyVideoViewPreparedListener = new MediaPlayer.OnPreparedListener() {

    @Override
    public void onPrepared(MediaPlayer mp) {

        long duration = myVideoView.getDuration(); // in millisecond
        Toast.makeText(MainActivity.this,
                "Duration: " + duration + " (ms)", Toast.LENGTH_LONG)
                .show();

    }
};

MediaPlayer.OnErrorListener myVideoViewErrorListener = new MediaPlayer.OnErrorListener() {

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {

        Toast.makeText(MainActivity.this, "Error!!!", Toast.LENGTH_LONG)
                .show();
        return true;
    }
};

}

You can Read More About it at Here http://android-er.blogspot.com/2013/05/get-current-frame-in-videoview-using.html