我已经得到了它创建从我的视频缩略图。 在code是这样的:
videoGalleryThumbnails.add(ThumbnailUtils.extractThumbnail(ThumbnailUtils.createVideoThumbnail( videoFile.getAbsolutePath(),MediaStore.Images.Thumbnails.MINI_KIND),500,200));
但创造的缩略图是一个非常糟糕的时间。这是什么时候的视频是黑色的。现在我有没有用一个全黑的缩略图的。
我怎样才能把我的视频的缩略图,在一个特定的时间?例如。在00:31或01:44?
或者是不可能的?
我试图也用类MediaMetadataRetriever,但我得到的只是一个白色的图像。 code看起来像这样
文件tempVideoList [] =(Environment.getExternalStoragePublicDirectory(PATH_VIDEO_GALLERY))
.listFiles();
类MediaMetadataRetriever猎犬=新类MediaMetadataRetriever();
位图MYBITMAP = NULL;
对于(文件videoFile:tempVideoList){
如果(videoFile.isFile()){
//从这里
尝试 {
retriever.setDataSource(videoFile.getAbsolutePath());
MYBITMAP = retriever.getFrameAtTime(11); //在第11秒
}赶上(例外前){
Log.i(MyDebug code,类MediaMetadataRetriever了异常:+前);
}
videoGalleryThumbnails.add(MYBITMAP);
在这里//
}
如果我更换code标记为从这里开始到这里的第一个顶部code,它的工作原理。 我也试过MediaMetadataRetriever.OPTION_CLOSEST和OPTION_CLOSEST_SYNC和OPTION_NEXT_SYNC。
解决方案好吧,我知道了。 本类MediaMetadataRetriever绝对是正确的道路要走。的问题是,它计数以微秒,而不是在秒的时间。解决方法是这样的:
尝试{
retriever.setDataSource(videoFile.getAbsolutePath());
INT timeInSeconds = 30;
MYBITMAP = retriever.getFrameAtTime(timeInSeconds * 1000000,
MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
}赶上(例外前){
Log.i(MyDebug code,类MediaMetadataRetriever了异常:+前);
}
videoGalleryThumbnails.add(MYBITMAP);
我不知道,如果OPTION_CLOSEST_SYNC实际需要,但看起来这是编程的更好的方法。
感谢威廉·赖利指着我在正确的方向。
I already got it to create a thumbnail from my video. The code looks like this:
videoGalleryThumbnails.add(ThumbnailUtils.extractThumbnail(ThumbnailUtils.createVideoThumbnail(
videoFile.getAbsolutePath(), MediaStore.Images.Thumbnails.MINI_KIND), 500, 200));
But the thumbnail created is at a really bad time. It is exactly when the video is black. Now i have no use of a completely black Thumbnail.
How can i take a Thumbnail of my video at a specific time? E.g. at 00:31 or at 01:44?
Or is it not possible?
I tried also to use MediaMetadataRetriever, but i get only a white image. Code looks like this
File tempVideoList[] = (Environment.getExternalStoragePublicDirectory(PATH_VIDEO_GALLERY))
.listFiles();
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Bitmap myBitmap=null;
for (File videoFile : tempVideoList) {
if (videoFile.isFile()) {
//from here
try {
retriever.setDataSource(videoFile.getAbsolutePath());
myBitmap = retriever.getFrameAtTime(11); //at 11th second
} catch (Exception ex) {
Log.i("MyDebugCode", "MediaMetadataRetriever got exception:" + ex);
}
videoGalleryThumbnails.add(myBitmap);
//to here
}
If i replace the code marked as "from here" to "to here" with the top first code, it works. I also tried MediaMetadataRetriever.OPTION_CLOSEST and OPTION_CLOSEST_SYNC and OPTION_NEXT_SYNC.
解决方案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.