Android Create Video Thumbnail at specific time

后端 未结 2 1608
天命终不由人
天命终不由人 2021-01-05 09:10

I already got it to create a thumbnail from my video. The code looks like this:

videoGalleryThumbnails.add(ThumbnailUtils.extractThumbnail(ThumbnailUtils.cre         


        
相关标签:
2条回答
  • 2021-01-05 09:19

    need little changes for this code:

    try {
    
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    

    //have to control the version for the setDataSource

       if (Build.VERSION.SDK_INT >= 14)
           retriever.setDataSource(video_path, new HashMap<String, String>());
       else
           retriever.setDataSource(video_path);
       int timeInSeconds = 5;
       Bitmap thumb = retriever.getFrameAtTime(timeInSeconds * 1000000,
                                    MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
       imageViewThumb.setImageBitmap(thumb);
       } catch (Exception ex) {
            ex.printStackTrace();
       }
    

    if we don't control the version for "setDataSource" then we will get exceptions. for me it was not working until version controlling.

    0 讨论(0)
  • 2021-01-05 09:37

    Ok i got it. The MediaMetadataRetriever was absolutely the right way to go. The problem is, that it counts the time in microseconds and not in seconds. Solution looks like this:

    try {
            retriever.setDataSource(videoFile.getAbsolutePath());
            int timeInSeconds = 30;
            myBitmap = retriever.getFrameAtTime(timeInSeconds * 1000000,
                        MediaMetadataRetriever.OPTION_CLOSEST_SYNC); 
        } catch (Exception ex) {
            Log.i("MyDebugCode", "MediaMetadataRetriever got exception:" + ex);
        }
    
    videoGalleryThumbnails.add(myBitmap);
    

    I don't know, if OPTION_CLOSEST_SYNC is actually needed, but it looks like this is the better way for programming.

    Thanks to William Riley for pointing me in the right direction.

    0 讨论(0)
提交回复
热议问题