Android音视频开发 - MediaMetadataRetriever 相关

这篇具有很好参考价值的文章主要介绍了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 images
        at 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()

本文由博客一文多发平台 OpenWrite 发布!文章来源地址https://www.toymoban.com/news/detail-844288.html

到了这里,关于Android音视频开发 - MediaMetadataRetriever 相关的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 音视频开发系列(6)——全面了解Android MediaFormat

    MediaFormat 是 Android 平台中用于描述音视频格式的类,它提供了许多 API 用于设置和获取音视频的格式信息。以下是 MediaFormat 类的主要 API: 用于创建音频和视频格式的 MediaFormat 对象。需要指定媒体类型(例如 audio/mp4a-latm 或 video/avc)、媒体的采样率、通道数、码率、帧率等信

    2024年02月01日
    浏览(29)
  • Android开源计划-一周开发app,webrtc音视频开发

    题目 – 一周开发app计划 首批参与成员 -小巫 -墨香 -梦痕 -边城刀客 -徐cc 要求 – -每位认领者按照开源规范来做,代码规范和Android开发规范 -每位认领者必须拥有github账号,熟练使用git对代码进来管理 -每个人认领一个功能点或模块 -提出完善的解决方案并提供封装良好的库

    2024年04月08日
    浏览(35)
  • Android音视频开发(三)——MediaExtractor和MediaMuxer的使用

    了解了音视频的编解码过程,我们接下来使用一下经常跟MediaCodec一起搭配的MediaExtractor和MediaMuxer。最后会使用一个简单的demo来了解具体了解这两个工具类的使用过程。这一节我们就先不讲MediaCodec了,放到下节的demo。 Android提供了一个MediaExtractor类,可以用来 分离容器中的

    2024年02月01日
    浏览(56)
  • Android 音视频开发实践系列-06-初步了解H.264视频编解码技术标准

    本文来自笔者本人的语雀博客,由于语雀升级后不再满足笔者的需求,因此之后笔者会陆续将一些之前已经发布但尚有价值的文章搬家到CSDN。 作为音视频行业从业者,怎么能不理解H.264视频编解码技术标准?本篇文章主要记录笔者学习过程中对众多优秀博客内容的摘抄整理,

    2023年04月09日
    浏览(38)
  • 【学习】从零开发的Android音视频开发(13)——MediaCodec到OMX框架过程及其硬解码

    在讲NuPlayer时,NuPlayer解码部分会创建MediaCodec,并且最终到达OMX框架,先看MediaCodec的 init 函数 从init函数中可以看到,首先创建了 ACodec ,并且初始化了 ALooper 、 AMessage ,由于ACodec继承自 AHandler ,那么一套消息机制就有了。最后发送 kWhatInit 消息,收到消息的逻辑位于ACodec.

    2023年04月08日
    浏览(37)
  • 音视频开发之旅——音频基础概念、交叉编译原理和实践(LAME的交叉编译)(Android)

    本文章已授权微信公众号郭霖(guolin_blog)转载。 本文主要讲解的是 音频基础概念 、 交叉编译原理和实践(LAME的交叉编译) ,是基于 Android平台 ,示例代码如下所示: AndroidAudioDemo 另外, iOS平台 也有相关的文章,如下所示: 音视频开发之旅——音频基础概念、交叉编译

    2024年04月25日
    浏览(35)
  • Android开发音视频方向学习路线及资源分享,学完还怕什么互联网寒冬?

    好了,回归正题。 光看大纲,大家都知道要学习音视频录制,编码,处理,但是具体不知道怎么做,也不知道怎么入门。我自己在入门的时候也一样,靠着搜索引擎自己一点一点的积累,在这里当然要谢谢在该领域无私奉献的大佬们。所以在这里,我会对知识进行细化,运用

    2024年04月11日
    浏览(39)
  • 【学习】从零开始的Android音视频开发(3)——MediaPlayer的prepare/prepareAsync流程和start流程

    在之前的流程中我们没有从MediaPlayer生态上认识各类库之间的依赖调用关系 MediaPlayer部分头文件在frameworks/base/include/media/目录中,这个目录和libmedia.so库源文件的目录frameworks/av/media/libmedia/相对应。主要头文件有 IMediaPlayerClient.h、mediaplayer.h、IMediaPlayer.h、IMediaPlayerService.h、Med

    2024年02月03日
    浏览(30)
  • 真-浅浅了解下音视频文件格式和相关概念

    散装知识,只是突然对这类知识感兴趣,想简单了解下,找到啥就记录啥,没有深入研究文件内部组成构造和底层实现技术和相关标准,毕竟内容挺多的,我也不是必须得学透,况且我没有chatGPT那样的”大脑\\\"…总而言之,门外汉,满足下自己浅浅的好奇心。 1、 MP4(MPEG-4

    2023年04月08日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包