Search code examples
androidframeandroid-videoviewscreen-capture

retriever.getFrameAtTime is not accurate, how can i get screenshot of video?


This is the function i am using:

public Bitmap getVideoFrame(String FD, long time) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(FD);
        return retriever.getFrameAtTime(time * 1000, MediaMetadataRetriever.OPTION_CLOSEST);
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    } catch (RuntimeException ex) {
        ex.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }
    return null;
}

where: FD is the path of the video time = videoview.getCurrentPosition();

The problem is that this function does not get the exact frame i need to get. on a longer video (20 min) it will be more accurate than in a short video (10-20 seconds). Is there something i am missing, if not, what could I use instead of the retriever?


Solution

  • ImageView imageView1=(ImageView)findViewById(R.id.imageView1);
    
    Bitmap bm=ThumbnailUtils.createVideoThumbnail(Environment.getExternalStorageDirectory() + "/videocapture.3gp",Thumbnails.MICRO_KIND);
    
    imageView1.setImageBitmap(bm);