连笔字作品 | 连笔字知识 | 加入收藏 连笔字转换器软件可转换多种连笔字在线预览 网页版 V2.0
连笔字转换器

当前位置:连笔字网 > 知识库 >

retriever,Android使用MediaMetadataRetriever类获取视频帧

时间:2023-11-30 22:44:31 编辑:连笔君 来源:连笔字网

Android使用MediaMetadataRetriever类获取视频帧时一直无法正确读取路径文件,iamge显示位null

从API 8开始,新增了一个类:
android.media.ThumbnailUtils这个类提供了3个静态方法一个用来获取第一帧得到的Bitmap,2个对图片进行缩略处理。
public static Bitmap createVideoThumbnail (String filePath, int kind)

第一个参数是文件的路径,第二个参数是指定图片的大小,有两种选择Thumbnails.MINI_KIND与Thumbnails.MICRO_KIND。
第一种文档上说大小是512 x 384 ,我用一个MP4格式文件测试得到544 x 960,用一个wmv格式文件测试得到160 x 120。明显不靠谱。第二种参数两种格式文件得到的大小都是 96 x 96,这个才是缩略图。extractThumbnail(Bitmap source, int width, int height, int options)
extractThumbnail(Bitmap source, int width, int height)

这两种方法都是用来处理Bitmap的大小的,第一个参数是要处理的Bitmap,第二个参数是处理后宽度,第三个是高度,第四个参数options,如果options定义为OPTIONS_RECYCLE_INPUT,则回收资源。也就是说可以用第三种方法把截取到的第一帧的Bitmap转成任意想要的大小,第三个方法还可以获取内存卡内图片的缩略图。
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path1, Thumbnails.MINI_KIND);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, 210, 210);

从API 10开始新增一类MediaMetadataRetriever可以用来获取媒体文件的信息
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(/sdcard/33.mp4);
Bitmap bitmap = mmr.getFrameAtTime();
image.setImageBitmap(bitmap);
System.out.println(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE)+);
System.out.println(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE)+);
mmr.release();

MediaMetadataRetriever可以获取任何一帧的缩略图。

public static Bitmap createVideoThumbnail(String filePath) {
// MediaMetadataRetriever is available on API Level 8
// but is hidden until API Level 10
Class clazz = null;
Object instance = null;
try {
clazz = Class.forName(android.media.MediaMetadataRetriever);
instance = clazz.newInstance();

Method method = clazz.getMethod(setDataSource, String.class);
method.invoke(instance, filePath);

// The method name changes between API Level 9 and 10.
if (Build.VERSION.SDK_INT <= 9) {
return (Bitmap) clazz.getMethod(captureFrame).invoke(instance);
} else {
byte[] data = (byte[]) clazz.getMethod(getEmbeddedPicture).invoke(instance);
if (data != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
if (bitmap != null) return bitmap;
}
return (Bitmap) clazz.getMethod(getFrameAtTime).invoke(instance);
}
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
} catch (InstantiationException e) {
Log.e(TAG, createVideoThumbnail, e);
} catch (InvocationTargetException e) {
Log.e(TAG, createVideoThumbnail, e);
} catch (ClassNotFoundException e) {
Log.e(TAG, createVideoThumbnail, e);
} catch (NoSuchMethodException e) {
Log.e(TAG, createVideoThumbnail, e);
} catch (IllegalAccessException e) {
Log.e(TAG, createVideoThumbnail, e);
} finally {
try {
if (instance != null) {
clazz.getMethod(release).invoke(instance);
}
} catch (Exception ignored) {
}
}
return null;
}

扩展

用Environment.getExternalStorageDirectory().getPath()+"/juyaling.mp4"时能进行视频videoView的播放,但提取帧时报错:路径无效。

Android中怎么逐帧读取视频文件

MediaMetadataRetriever media = new MediaMetadataRetriever();
media.setDataSource(”视频的绝对地址“);
Bitmap bitmap = media.getFrameAtTime();
image = (ImageView)this.findViewById(R.id.imageView1);
image.setImageBitmap(bitmap);

这个是获取第一祯的代码,可以参考下

Android MediaMetadataRetriever.getFrameAtTime()提供的函数是不是系统下层没给实现,无论怎么改变时间

我记得我以前犯过这个错, getFrameAtTime() 是标准时间,也就是从1970年至今的毫秒数,知道为什么会只显示第一帧了吧,我估计你是传比如1000这样的值,还不到当前时间,当然是第一帧了。

扩展

了解你的意思,我试试。

行不通哦,之前查了文档,貌似是说该方法在底层直接给return null了。

其实这个方法的这个时间,指的是播放视频的总时长中具体时间的具体的位置。

Android 视频开发中如何通过url或者本地视

第一步:将bitmap转换成drawable对象,并设置给surfaceView视频播放窗口作为背景图片
//通过getVideoThumbnail方法取得视频中的第一帧图片,该图片是一个bitmap对象Bitmap bitmap=getVideoThumbnail(String url);//将bitmap对象转换成drawable对象Drawable drawable=new BitmapDrawable(bitmap);//将drawable对象设置给视频播放窗口surfaceView控件作为背景图片surfaceView.setBackgroundDrawable(drawable);123456

第二部分:通过url网址或者本地文件路径获得视频的第一帧图片
public Bitmap getVideoThumbnail(String url) {
Bitmap bitmap = null;//MediaMetadataRetriever 是android中定义好的一个类,提供了统一//的接口,用于从输入的媒体文件中取得帧和元数据;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
//()根据文件路径获取缩略图//retriever.setDataSource(filePath);
retriever.setDataSource(url, new HashMap()); //获得第一帧图片
bitmap = retriever.getFrameAtTime();
}
catch(IllegalArgumentException e) {
e.printStackTrace();
}
catch (RuntimeException e) {
e.printStackTrace();
}
finally {
try {
retriever.release();
}
catch (RuntimeException e) {
e.printStackTrace();
}
}
Log.v("bitmap", "bitmap="+bitmap); return bitmap;
}

Android 编程中,MediaRecord中据说有个Icamera可以获取视频流,详细如下

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);

扩展

能不能给个详细点的例子?

Copyright:2022-2023 连笔字转换器 www.liulisui.com All rights reserved.