当前位置: 首页 > 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框架的…...

生成xcframework

打包 XCFramework 的方法 XCFramework 是苹果推出的一种多平台二进制分发格式,可以包含多个架构和平台的代码。打包 XCFramework 通常用于分发库或框架。 使用 Xcode 命令行工具打包 通过 xcodebuild 命令可以打包 XCFramework。确保项目已经配置好需要支持的平台…...

Zustand 状态管理库:极简而强大的解决方案

Zustand 是一个轻量级、快速和可扩展的状态管理库,特别适合 React 应用。它以简洁的 API 和高效的性能解决了 Redux 等状态管理方案中的繁琐问题。 核心优势对比 基本使用指南 1. 创建 Store // store.js import create from zustandconst useStore create((set)…...

macOS多出来了:Google云端硬盘、YouTube、表格、幻灯片、Gmail、Google文档等应用

文章目录 问题现象问题原因解决办法 问题现象 macOS启动台(Launchpad)多出来了:Google云端硬盘、YouTube、表格、幻灯片、Gmail、Google文档等应用。 问题原因 很明显,都是Google家的办公全家桶。这些应用并不是通过独立安装的…...

Linux C语言网络编程详细入门教程:如何一步步实现TCP服务端与客户端通信

文章目录 Linux C语言网络编程详细入门教程:如何一步步实现TCP服务端与客户端通信前言一、网络通信基础概念二、服务端与客户端的完整流程图解三、每一步的详细讲解和代码示例1. 创建Socket(服务端和客户端都要)2. 绑定本地地址和端口&#x…...

人机融合智能 | “人智交互”跨学科新领域

本文系统地提出基于“以人为中心AI(HCAI)”理念的人-人工智能交互(人智交互)这一跨学科新领域及框架,定义人智交互领域的理念、基本理论和关键问题、方法、开发流程和参与团队等,阐述提出人智交互新领域的意义。然后,提出人智交互研究的三种新范式取向以及它们的意义。最后,总结…...

LRU 缓存机制详解与实现(Java版) + 力扣解决

📌 LRU 缓存机制详解与实现(Java版) 一、📖 问题背景 在日常开发中,我们经常会使用 缓存(Cache) 来提升性能。但由于内存有限,缓存不可能无限增长,于是需要策略决定&am…...

Proxmox Mail Gateway安装指南:从零开始配置高效邮件过滤系统

💝💝💝欢迎莅临我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。 推荐:「storms…...

pycharm 设置环境出错

pycharm 设置环境出错 pycharm 新建项目,设置虚拟环境,出错 pycharm 出错 Cannot open Local Failed to start [powershell.exe, -NoExit, -ExecutionPolicy, Bypass, -File, C:\Program Files\JetBrains\PyCharm 2024.1.3\plugins\terminal\shell-int…...

6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础

第三周 Day 3 🎯 今日目标 理解类(class)和对象(object)的关系学会定义类的属性、方法和构造函数(init)掌握对象的创建与使用初识封装、继承和多态的基本概念(预告) &a…...

Sklearn 机器学习 缺失值处理 获取填充失值的统计值

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 使用 Scikit-learn 处理缺失值并提取填充统计信息的完整指南 在机器学习项目中,数据清…...