如何从rtsp流中截取Android的VideoView屏幕截图?

时间:2014-07-28 07:02:32

标签: android screenshot android-videoview rtsp

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();

如何拍摄截图? 我尝试截图,但图片是黑色的。

1 个答案:

答案 0 :(得分:2)

这是捕捉视频中当前帧的截图

的方法
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;
    }
};

}
  

您可以在此处详细了解   http://android-er.blogspot.com/2013/05/get-current-frame-in-videoview-using.html