当前位置: 首页 > news >正文

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。 特性: 集合的目的是将不同的值存放在一起,不同的集合间用来做关系运算,无须纠结于集合中的单个值。 &#xff0…...

实用技巧:如何取消app的截屏禁用

因为我想要在小鹅通App做笔记,但是被小鹅通App禁用截屏了,这真是一个很糟糕的使用体验,虽然可能是为了保护商家权益…… 方法1 可以让商家设置课程可以截屏 方法2 手机root,安装Xposed框架,利用Xposed框架上面的插件我们可以对手机进行高度定制化,而安装Xposed框架的…...

云原生核心技术 (7/12): K8s 核心概念白话解读(上):Pod 和 Deployment 究竟是什么?

大家好,欢迎来到《云原生核心技术》系列的第七篇! 在上一篇,我们成功地使用 Minikube 或 kind 在自己的电脑上搭建起了一个迷你但功能完备的 Kubernetes 集群。现在,我们就像一个拥有了一块崭新数字土地的农场主,是时…...

[ICLR 2022]How Much Can CLIP Benefit Vision-and-Language Tasks?

论文网址:pdf 英文是纯手打的!论文原文的summarizing and paraphrasing。可能会出现难以避免的拼写错误和语法错误,若有发现欢迎评论指正!文章偏向于笔记,谨慎食用 目录 1. 心得 2. 论文逐段精读 2.1. Abstract 2…...

Java-41 深入浅出 Spring - 声明式事务的支持 事务配置 XML模式 XML+注解模式

点一下关注吧!!!非常感谢!!持续更新!!! 🚀 AI篇持续更新中!(长期更新) 目前2025年06月05日更新到: AI炼丹日志-28 - Aud…...

Android15默认授权浮窗权限

我们经常有那种需求,客户需要定制的apk集成在ROM中,并且默认授予其【显示在其他应用的上层】权限,也就是我们常说的浮窗权限,那么我们就可以通过以下方法在wms、ams等系统服务的systemReady()方法中调用即可实现预置应用默认授权浮…...

【C++从零实现Json-Rpc框架】第六弹 —— 服务端模块划分

一、项目背景回顾 前五弹完成了Json-Rpc协议解析、请求处理、客户端调用等基础模块搭建。 本弹重点聚焦于服务端的模块划分与架构设计,提升代码结构的可维护性与扩展性。 二、服务端模块设计目标 高内聚低耦合:各模块职责清晰,便于独立开发…...

JVM虚拟机:内存结构、垃圾回收、性能优化

1、JVM虚拟机的简介 Java 虚拟机(Java Virtual Machine 简称:JVM)是运行所有 Java 程序的抽象计算机,是 Java 语言的运行环境,实现了 Java 程序的跨平台特性。JVM 屏蔽了与具体操作系统平台相关的信息,使得 Java 程序只需生成在 JVM 上运行的目标代码(字节码),就可以…...

链式法则中 复合函数的推导路径 多变量“信息传递路径”

非常好,我们将之前关于偏导数链式法则中不能“约掉”偏导符号的问题,统一使用 二重复合函数: z f ( u ( x , y ) , v ( x , y ) ) \boxed{z f(u(x,y),\ v(x,y))} zf(u(x,y), v(x,y))​ 来全面说明。我们会展示其全微分形式(偏导…...

大数据驱动企业决策智能化的路径与实践

📝个人主页🌹:慌ZHANG-CSDN博客 🌹🌹期待您的关注 🌹🌹 一、引言:数据驱动的企业竞争力重构 在这个瞬息万变的商业时代,“快者胜”的竞争逻辑愈发明显。企业如何在复杂环…...

C++11 constexpr和字面类型:从入门到精通

文章目录 引言一、constexpr的基本概念与使用1.1 constexpr的定义与作用1.2 constexpr变量1.3 constexpr函数1.4 constexpr在类构造函数中的应用1.5 constexpr的优势 二、字面类型的基本概念与使用2.1 字面类型的定义与作用2.2 字面类型的应用场景2.2.1 常量定义2.2.2 模板参数…...

iOS 项目怎么构建稳定性保障机制?一次系统性防错经验分享(含 KeyMob 工具应用)

崩溃、内存飙升、后台任务未释放、页面卡顿、日志丢失——稳定性问题,不一定会立刻崩,但一旦积累,就是“上线后救不回来的代价”。 稳定性保障不是某个工具的功能,而是一套贯穿开发、测试、上线全流程的“观测分析防范”机制。 …...