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 相关
Android音视频开发 - MediaMetadataRetriever 相关 MediaMetadataRetriever 是android中用于从媒体文件中提取元数据新的类. 可以获取音频,视频和图像文件的各种信息,如时长,标题,封面等. 1:初始化对象 private MediaMetadataRetriever mediaMetadataRetriever new MediaMe…...

注解(Annotation)
10.1 注解概述 10.1.1 什么是注解 注解(Annotation)是从JDK5.0开始引入,以“注解名”在代码中存在。例如: Override Deprecated SuppressWarnings(value”unchecked”) Annotation 可以像修饰符一样被使用,可用于修饰…...

蓝桥杯:七步诗 ← bfs
【题目来源】https://www.lanqiao.cn/problems/3447/learning/【题目描述】 煮豆燃豆苴,豆在釜中泣。本是同根生,相煎何太急?---曹植 所以,这道题目关乎豆子! 话说赤壁之战结束后,曹操的船舰被刘备烧了,引领军队从华容…...

Vue 如何快速上手
目录 1. Vue 是什么 (概念) 1.1. Vue 的两种使用方式 1.2. 优点 1.3. 缺点 2. 创建 Vue 实例,初始化渲染 2.1. 步骤(核心步骤 4步) 2.2. 练习——创建一个Vue实例 3. 插值表达式 {{ }} 3.1. 介绍 3.2. 作用…...
Vue3:组件间通信-provide和inject实现祖先组件与后代组件间直接通信
一、情景说明 我们学习了很多的组件间通信 这里在学习一种,祖先组件与后代组件间通信的技术 这里的后代,可以是多层继承关系,子组件,子子组件,子子子组件等等。 在祖先组件中通过provide配置向后代组件提供数据在后代…...
微信小程序——小程序和页面生命周期详解
小程序的生命周期 小程序的生命周期主要分为以下几个阶段: 创建(onLoad): 当小程序启动时,或者从其他页面跳转到当前页面时,会触发 onLoad 生命周期函数。 这个阶段通常用于初始化页面数据,从服…...
android studio中添加module依赖
android常用的三种依赖 库依赖(Library dependency):以访问网址的形式将依赖库相应版本下载到本地; 文件依赖(File dependency): 将下载下来的依赖库以.jar文件的形式添加依赖. module依赖(Modu…...

【.NET全栈】.NET全栈学习路线
一、微软官方C#学习 https://learn.microsoft.com/zh-cn/dotnet/csharp/tour-of-csharp/ C#中的数据类型 二、2021 ASP.NET Core 开发者路线图 GitHub地址:https://github.com/MoienTajik/AspNetCore-Developer-Roadmap/blob/master/ReadMe.zh-Hans.md 三、路线…...

代码随想录阅读笔记-二叉树【二叉搜索树中的搜索】
题目 给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。 例如, 在上述示例中,如果要找的值是 5,但因为没有节点…...
1、初识drf
drf的学习需要学习者有django基本使用知识。 文章目录 什么是drf,有什么作用CBV是什么初步使用drf 下载以及django创建项目django最小启动内容修改setting修改 url 编写drf视图编辑url测试返回结果 什么是drf,有什么作用 drf(django rest-framework),让…...
速盾:cdn高防御服务器租用有哪些好处
随着互联网的发展,网络安全问题日益突出。攻击者利用各种手段不断对网站进行攻击,给网站的安全运行带来威胁。为了保障网站的正常运行和数据的安全,越来越多的网站开始租用CDN高防御服务器。那么,租用CDN高防御服务器有哪些好处呢…...
【跟小嘉学 Linux 系统架构与开发】四、文件和目录的权限
系列文章目录 【跟小嘉学 Linux 系统架构与开发】一、学习环境的准备与Linux系统介绍 【跟小嘉学 Linux 系统架构与开发】二、Linux发型版介绍与基础常用命令介绍 【跟小嘉学 Linux 系统架构与开发】三、如何查看帮助文档 【跟小嘉学 Linux 系统架构与开发】四、文件和目录的权…...

ubuntu18.04图形界面卡死,鼠标键盘失灵, 通过MAC共享网络给Ubuntu解决!
ubuntu18.04图形界面卡死,鼠标键盘失灵, 通过MAC共享网络给Ubuntu解决! 1. 尝试从卡死的图形界面切换到命令行界面2. 进入bios和grub页面3. 更改Grub中的设置,以进入命令行4. 在命令行页面解决图形界面卡死的问题5. Mac共享WI-FI网…...

ESG认证(ESG=环境、社会和治理 Environmental, Social, and Governance)
什么是ESG认证 ESG认证是指根据企业在环境、社会和治理(Environmental, Social, and Governance)方面的表现而设立的一种评价或评级体系。 环境(Environmental):这一维度关注企业如何管理其对环境的影响,包…...
Cesium Viewer 类学习
Viewer提供了创建和控制3D场景所需的所有基本功能,包括加载3D模型、添加图像覆盖物、设置相机位置和方向、处理用户输入等。 构造函数: new Cesium.Viewer(container, options) 是用来创建一个新的 Cesium 视图器(Viewer)实例的…...

第十四届省赛大学B组(C/C++)子串简写
原题链接:子串简写 程序猿圈子里正在流行一种很新的简写方法: 对于一个字符串,只保留首尾字符,将首尾字符之间的所有字符用这部分的长度代替。 例如 internationalization 简写成 i18n,Kubernetes 简写成 K8s&#…...

深入浅出 -- 系统架构之微服务架构
1.1 微服务的架构特征: 单一职责:微服务拆分粒度更小,每一个服务都对应唯一的业务能力,做到单一职责 自治:团队独立、技术独立、数据独立,独立部署和交付 面向服务:服务提供统一标准的接口&…...
YoloV8改进策略:下采样改进|自研下采样模块(独家改进)|疯狂涨点|附结构图
摘要 本文介绍我自研的下采样模块。本次改进的下采样模块是一种通用的改进方法,你可以用分类任务的主干网络中,也可以用在分割和超分的任务中。已经有粉丝用来改进ConvNext模型,取得了非常好的效果,配合一些其他的改进,发一篇CVPR、ECCV之类的顶会完全没有问题。 本次我…...

Python从0到100(十):Python集合介绍及运用
一、集合定义 定义: 由不同元素组成的集合,集合是一组无序排列 可hash值,可作为字典的key。 特性: 集合的目的是将不同的值存放在一起,不同的集合间用来做关系运算,无须纠结于集合中的单个值。 ࿰…...

实用技巧:如何取消app的截屏禁用
因为我想要在小鹅通App做笔记,但是被小鹅通App禁用截屏了,这真是一个很糟糕的使用体验,虽然可能是为了保护商家权益…… 方法1 可以让商家设置课程可以截屏 方法2 手机root,安装Xposed框架,利用Xposed框架上面的插件我们可以对手机进行高度定制化,而安装Xposed框架的…...
Cursor实现用excel数据填充word模版的方法
cursor主页:https://www.cursor.com/ 任务目标:把excel格式的数据里的单元格,按照某一个固定模版填充到word中 文章目录 注意事项逐步生成程序1. 确定格式2. 调试程序 注意事项 直接给一个excel文件和最终呈现的word文件的示例,…...
脑机新手指南(八):OpenBCI_GUI:从环境搭建到数据可视化(下)
一、数据处理与分析实战 (一)实时滤波与参数调整 基础滤波操作 60Hz 工频滤波:勾选界面右侧 “60Hz” 复选框,可有效抑制电网干扰(适用于北美地区,欧洲用户可调整为 50Hz)。 平滑处理&…...
Java 8 Stream API 入门到实践详解
一、告别 for 循环! 传统痛点: Java 8 之前,集合操作离不开冗长的 for 循环和匿名类。例如,过滤列表中的偶数: List<Integer> list Arrays.asList(1, 2, 3, 4, 5); List<Integer> evens new ArrayList…...
鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院挂号小程序
一、开发准备 环境搭建: 安装DevEco Studio 3.0或更高版本配置HarmonyOS SDK申请开发者账号 项目创建: File > New > Create Project > Application (选择"Empty Ability") 二、核心功能实现 1. 医院科室展示 /…...
django filter 统计数量 按属性去重
在Django中,如果你想要根据某个属性对查询集进行去重并统计数量,你可以使用values()方法配合annotate()方法来实现。这里有两种常见的方法来完成这个需求: 方法1:使用annotate()和Count 假设你有一个模型Item,并且你想…...
vue3 定时器-定义全局方法 vue+ts
1.创建ts文件 路径:src/utils/timer.ts 完整代码: import { onUnmounted } from vuetype TimerCallback (...args: any[]) > voidexport function useGlobalTimer() {const timers: Map<number, NodeJS.Timeout> new Map()// 创建定时器con…...

排序算法总结(C++)
目录 一、稳定性二、排序算法选择、冒泡、插入排序归并排序随机快速排序堆排序基数排序计数排序 三、总结 一、稳定性 排序算法的稳定性是指:同样大小的样本 **(同样大小的数据)**在排序之后不会改变原始的相对次序。 稳定性对基础类型对象…...
站群服务器的应用场景都有哪些?
站群服务器主要是为了多个网站的托管和管理所设计的,可以通过集中管理和高效资源的分配,来支持多个独立的网站同时运行,让每一个网站都可以分配到独立的IP地址,避免出现IP关联的风险,用户还可以通过控制面板进行管理功…...
0x-3-Oracle 23 ai-sqlcl 25.1 集成安装-配置和优化
是不是受够了安装了oracle database之后sqlplus的简陋,无法删除无法上下翻页的苦恼。 可以安装readline和rlwrap插件的话,配置.bahs_profile后也能解决上下翻页这些,但是很多生产环境无法安装rpm包。 oracle提供了sqlcl免费许可,…...

保姆级【快数学会Android端“动画“】+ 实现补间动画和逐帧动画!!!
目录 补间动画 1.创建资源文件夹 2.设置文件夹类型 3.创建.xml文件 4.样式设计 5.动画设置 6.动画的实现 内容拓展 7.在原基础上继续添加.xml文件 8.xml代码编写 (1)rotate_anim (2)scale_anim (3)translate_anim 9.MainActivity.java代码汇总 10.效果展示 逐帧…...