This question already has an answer here:
I was able to figure out how to capture a video frame from a VideoView that is playing a video stored locally on the phone. However, when the video is being streamed over IP in the VideoView, it appears very difficult to capture a screenshot/image/video frame. I would appreciate a solution to this problem.
Here is a similar question which has not received an answer: How to capture a frame from video in android?
Here is a solution to capturing the video frame (ONLY if the video is stored on the phone):
public static Bitmap captureVideoFrame(Activity activity, VideoView vv, String viewSource, int currPosInMs)
{
MediaMetadataRetriever mmRetriever = new MediaMetadataRetriever();
mmRetriever.setDataSource(viewSource);
Bitmap bmFrame = mmRetriever.getFrameAtTime(currPosInMs * 1000); // unit in microsecond
if(bmFrame == null){
Toast.makeText(activity.getApplicationContext(), "Bitmap is null! Curr Position: " + currPosInMs, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(activity.getApplicationContext(), "Bitmap is not null! Curr Position: " + currPosInMs, Toast.LENGTH_SHORT).show();
// Save file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + SCREENSHOT_DIRECTORY + "myScreenshot.png";
OutputStream fout = null;
File imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bmFrame.compress(Bitmap.CompressFormat.JPEG, 100, fout);
fout.flush();
fout.close();
Toast.makeText(activity.getApplicationContext(), "Saved file successfully!", Toast.LENGTH_SHORT).show();
}
catch(Exception e){
}
}
return bmFrame;
}
I have found a solution to this problem. It appears that the VideoView does not allow this because of low-level hardware GPU reasons while using a SurfaceView.
The solution is to use a TextureView and use a MediaPlayer to play a video inside of it. The Activity will need to implement TextureView.SurfaceTextureListener. When taking a screenshot with this solution, the video freezes for a short while. Also, the TextureView does not display a default UI for the playback progress bar (play, pause, FF/RW, play time, etc). That is one drawback. If you have another solution please let me know :)
Here is the solution: