I've seen a lot of old questions about this, maybe now there are some solutions. I want to take a screenshot of the current frame of my videoview. Videoview shows a real-time video using a rtsp-stream.
I try to take bitmap, but it's always black
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width , v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
EDIT :
MediaMetadataRetriever
does not work with stream url, maybe works with video-file.
Using lib at this link (it's a wrapper of MediaMetadataRetriever
that enable rtsp protocol input) I can save a frame of video, but there is a delay of 10 secs respect real-time videoview because it must create a new connection with streaming server.
I not test ThumbnailUtils
, but in Api I read that input is only file-path
Use TextureView instead VideoView. TextureView has getBitmap() method. Here is the usage of TextureView as videoView
public class TextureVideoActivity extends Activity implements TextureView.SurfaceTextureListener {
private static final String FILE_NAME = "myVideo.mp4";
private static final String TAG = TextureVideoActivity.class.getName();
private MediaPlayer mMediaPlayer;
private TextureView mPreview;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_texture_video);
mPreview = (TextureView) findViewById(R.id.textureView);
mPreview.setSurfaceTextureListener(this);
}
public Bitmap getBitmap(){
return mPreview.getBitmap();
}
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer
.setDataSource(this, Uri.parse(FILE_NAME));
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
// don't forget to call MediaPlayer.prepareAsync() method when you use constructor for
// creating MediaPlayer
mMediaPlayer.prepareAsync();
// Play video when the media source is ready for playback.
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
// Make sure we stop video and release resources when activity is destroyed.
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
}
Playing video on TextureView
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With