Android音视频开发 - MediaMetadataRetriever 相关

2024-04-04 13:44

本文主要是介绍Android音视频开发 - MediaMetadataRetriever 相关,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android音视频开发 - MediaMetadataRetriever 相关

MediaMetadataRetriever 是android中用于从媒体文件中提取元数据新的类. 可以获取音频,视频和图像文件的各种信息,如时长,标题,封面等.

1:初始化对象

private MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource("sdcard/share.mp4");

需要申请读写权限.

这里我使用的是本地路径, 需要注意的是如果路径文件不存在,会抛出

IllegalArgumentException,具体的源码如下:

public void setDataSource(String path) throws IllegalArgumentException {if (path == null) {throw new IllegalArgumentException();}try (FileInputStream is = new FileInputStream(path)) {FileDescriptor fd = is.getFD();setDataSource(fd, 0, 0x7ffffffffffffffL);} catch (FileNotFoundException fileEx) {throw new IllegalArgumentException();} catch (IOException ioEx) {throw new IllegalArgumentException();}
}

2: extractMetadata

根据keyCode返回keyCode关联的元数据.

系统的keyCode如下:

 /*** The metadata key to retrieve the numeric string describing the* order of the audio data source on its original recording.*/public static final int METADATA_KEY_CD_TRACK_NUMBER = 0;/*** The metadata key to retrieve the information about the album title* of the data source.*/public static final int METADATA_KEY_ALBUM           = 1;/*** The metadata key to retrieve the information about the artist of* the data source.*/public static final int METADATA_KEY_ARTIST          = 2;/*** The metadata key to retrieve the information about the author of* the data source.*/public static final int METADATA_KEY_AUTHOR          = 3;/*** The metadata key to retrieve the information about the composer of* the data source.*/public static final int METADATA_KEY_COMPOSER        = 4;/*** The metadata key to retrieve the date when the data source was created* or modified.*/public static final int METADATA_KEY_DATE            = 5;/*** The metadata key to retrieve the content type or genre of the data* source.*/public static final int METADATA_KEY_GENRE           = 6;/*** The metadata key to retrieve the data source title.*/public static final int METADATA_KEY_TITLE           = 7;/*** The metadata key to retrieve the year when the data source was created* or modified.*/public static final int METADATA_KEY_YEAR            = 8;/*** The metadata key to retrieve the playback duration of the data source.*/public static final int METADATA_KEY_DURATION        = 9;/*** The metadata key to retrieve the number of tracks, such as audio, video,* text, in the data source, such as a mp4 or 3gpp file.*/public static final int METADATA_KEY_NUM_TRACKS      = 10;/*** The metadata key to retrieve the information of the writer (such as* lyricist) of the data source.*/public static final int METADATA_KEY_WRITER          = 11;/*** The metadata key to retrieve the mime type of the data source. Some* example mime types include: "video/mp4", "audio/mp4", "audio/amr-wb",* etc.*/public static final int METADATA_KEY_MIMETYPE        = 12;/*** The metadata key to retrieve the information about the performers or* artist associated with the data source.*/public static final int METADATA_KEY_ALBUMARTIST     = 13;/*** The metadata key to retrieve the numberic string that describes which* part of a set the audio data source comes from.*/public static final int METADATA_KEY_DISC_NUMBER     = 14;/*** The metadata key to retrieve the music album compilation status.*/public static final int METADATA_KEY_COMPILATION     = 15;/*** If this key exists the media contains audio content.*/public static final int METADATA_KEY_HAS_AUDIO       = 16;/*** If this key exists the media contains video content.*/public static final int METADATA_KEY_HAS_VIDEO       = 17;/*** If the media contains video, this key retrieves its width.*/public static final int METADATA_KEY_VIDEO_WIDTH     = 18;/*** If the media contains video, this key retrieves its height.*/public static final int METADATA_KEY_VIDEO_HEIGHT    = 19;/*** This key retrieves the average bitrate (in bits/sec), if available.*/public static final int METADATA_KEY_BITRATE         = 20;/*** This key retrieves the language code of text tracks, if available.* If multiple text tracks present, the return value will look like:* "eng:chi"* @hide*/public static final int METADATA_KEY_TIMED_TEXT_LANGUAGES      = 21;/*** If this key exists the media is drm-protected.* @hide*/public static final int METADATA_KEY_IS_DRM          = 22;/*** This key retrieves the location information, if available.* The location should be specified according to ISO-6709 standard, under* a mp4/3gp box "@xyz". Location with longitude of -90 degrees and latitude* of 180 degrees will be retrieved as "-90.0000+180.0000", for instance.*/public static final int METADATA_KEY_LOCATION        = 23;/*** This key retrieves the video rotation angle in degrees, if available.* The video rotation angle may be 0, 90, 180, or 270 degrees.*/public static final int METADATA_KEY_VIDEO_ROTATION = 24;/*** This key retrieves the original capture framerate, if it's* available. The capture framerate will be a floating point* number.*/public static final int METADATA_KEY_CAPTURE_FRAMERATE = 25;/*** If this key exists the media contains still image content.*/public static final int METADATA_KEY_HAS_IMAGE       = 26;/*** If the media contains still images, this key retrieves the number* of still images.*/public static final int METADATA_KEY_IMAGE_COUNT     = 27;/*** If the media contains still images, this key retrieves the image* index of the primary image.*/public static final int METADATA_KEY_IMAGE_PRIMARY   = 28;/*** If the media contains still images, this key retrieves the width* of the primary image.*/public static final int METADATA_KEY_IMAGE_WIDTH     = 29;/*** If the media contains still images, this key retrieves the height* of the primary image.*/public static final int METADATA_KEY_IMAGE_HEIGHT    = 30;/*** If the media contains still images, this key retrieves the rotation* angle (in degrees clockwise) of the primary image. The image rotation* angle must be one of 0, 90, 180, or 270 degrees.*/public static final int METADATA_KEY_IMAGE_ROTATION  = 31;/*** If the media contains video and this key exists, it retrieves the* total number of frames in the video sequence.*/public static final int METADATA_KEY_VIDEO_FRAME_COUNT = 32;/*** @hide*/public static final int METADATA_KEY_EXIF_OFFSET = 33;/*** @hide*/public static final int METADATA_KEY_EXIF_LENGTH = 34;// Add more here...

如获取视频时长:

String METADATA_KEY_DURATION = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
Log.i(TAG, "onCreate: METADATA_KEY_DURATION="+METADATA_KEY_DURATION);

3: getFrameAtTime

该方法在任何时间位置找到一个有代表性的帧,并将其作为位图返回.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {Bitmap frameAtTime =         mediaMetadataRetriever.getFrameAtTime();
}

如果需要获取指定时间,则可以调用

 public Bitmap getFrameAtTime(long timeUs) {return getFrameAtTime(timeUs, OPTION_CLOSEST_SYNC);}

4: getFrameAtIndex

用于从媒体文件中获取指定索引位置的帧图像.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {Bitmap frameAtIndex = mediaMetadataRetriever.getFrameAtIndex(0);
}

5: getImageAtIndex

基于0的图像索引,返回位图信息.

Bitmap imageAtIndex = mediaMetadataRetriever.getImageAtIndex(0);

这里调用该方法时,会抛出IllegalStateException :

  java.lang.IllegalStateException: Does not contail still imagesat android.media.MediaMetadataRetriever.getImageAtIndexInternal(MediaMetadataRetriever.java:648)at android.media.MediaMetadataRetriever.getImageAtIndex(MediaMetadataRetriever.java:605)at com.test.media.MainActivity.lambda$onCreate$0$MainActivity(MainActivity.java:50)at com.test.media.-$$Lambda$MainActivity$fGcBDHveSBN77vUeMp6H1nheePE.onClick(Unknown Source:2)at android.view.View.performClick(View.java:7259)at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967)at android.view.View.performClickInternal(View.java:7236)at android.view.View.access$3600(View.java:801)at android.view.View$PerformClick.run(View.java:27892)at android.os.Handler.handleCallback(Handler.java:894)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:214)at android.app.ActivityThread.main(ActivityThread.java:7356)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:940)

具体的错误信息的原因如下:

private Bitmap getImageAtIndexInternal(int imageIndex, @Nullable BitmapParams params) {if (!"yes".equals(extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_IMAGE))) {throw new IllegalStateException("Does not contail still images");}String imageCount = extractMetadata(MediaMetadataRetriever.METADATA_KEY_IMAGE_COUNT);if (imageIndex >= Integer.parseInt(imageCount)) {throw new IllegalArgumentException("Invalid image index: " + imageCount);}return _getImageAtIndex(imageIndex, params);
}

可以看到系统源码中校验了extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_IMAGE)的值,如果值不是"yes",就会抛出"Does not contail still images".

与getImageAtIndex类似的方法还有:

getImageAtIndex(int, BitmapParams)
getPrimaryImage(BitmapParams)
getPrimaryImage()

这篇关于Android音视频开发 - MediaMetadataRetriever 相关的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/875941

相关文章

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

基于Python开发PPTX压缩工具

《基于Python开发PPTX压缩工具》在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,不便于传输和存储,所以本文将使用Python开发一个PPTX压缩工具,需要的可以了解下... 目录引言全部代码环境准备代码结构代码实现运行结果引言在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

Redis的Zset类型及相关命令详细讲解

《Redis的Zset类型及相关命令详细讲解》:本文主要介绍Redis的Zset类型及相关命令的相关资料,有序集合Zset是一种Redis数据结构,它类似于集合Set,但每个元素都有一个关联的分数... 目录Zset简介ZADDZCARDZCOUNTZRANGEZREVRANGEZRANGEBYSCOREZ

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

关于Maven生命周期相关命令演示

《关于Maven生命周期相关命令演示》Maven的生命周期分为Clean、Default和Site三个主要阶段,每个阶段包含多个关键步骤,如清理、编译、测试、打包等,通过执行相应的Maven命令,可以... 目录1. Maven 生命周期概述1.1 Clean Lifecycle1.2 Default Li

numpy求解线性代数相关问题

《numpy求解线性代数相关问题》本文主要介绍了numpy求解线性代数相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 在numpy中有numpy.array类型和numpy.mat类型,前者是数组类型,后者是矩阵类型。数组