我是靠谱客的博主 英勇手套,最近开发中收集的这篇文章主要介绍Android MediaExtractor的使用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

我们知道多媒体文件当中包含很多流,比如视频流,音频流等。我们可以通过

MediaExtractor获取到相关的信息。下面是具体用法。
package com.yuanxuzhen.androidmedia.demux;

import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.yuanxuzhen.androidmedia.DirUtil;
import com.yuanxuzhen.androidmedia.databinding.ActivityDemuxBinding;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DemuxActivity extends AppCompatActivity {
    ActivityDemuxBinding mainBinding;
    ExecutorService mExecutorService;
    private String mediaPath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainBinding = ActivityDemuxBinding.inflate(getLayoutInflater());
        setContentView(mainBinding.getRoot());
        mExecutorService = Executors.newCachedThreadPool();
        mediaPath = DirUtil.getCacheDir() + File.separator + "in.mp4";
        mainBinding.print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mExecutorService.execute(new Runnable() {
                    @Override
                    public void run() {
                          /*打印通道信息*/
//                        printMediaCount(mediaPath);
//                        int videoIndex = getTrackIndex("video", mediaPath);
//                        printLog("videoIndex="+videoIndex);
                        printVideoMeidaInfo(mediaPath);
                        printAudioMeidaInfo(mediaPath);
                    }
                });
            }
        });
    }

    private void printLog(String msg){
        Log.e("DemuxAcYuan", msg);
    }
    private  void printMediaCount(String path){
        try{
            MediaExtractor extractor = new MediaExtractor();//实例一个MediaExtractor
            extractor.setDataSource(path);
            int count = extractor.getTrackCount();//获取轨道数量
            printLog("printMediaInfo count="+count);
            for (int i = 0; i < count; i++){
                MediaFormat mediaFormat = extractor.getTrackFormat(i);
                printLog(i+"编号通道格式 = "+mediaFormat.getString(MediaFormat.KEY_MIME));
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private int getTrackIndex(String targetTrack, String path) {
        MediaExtractor extractor = new MediaExtractor();//实例一个MediaExtractor
        try {
            extractor.setDataSource(path);//设置添加MP4文件路径
        } catch (IOException e) {
            e.printStackTrace();
        }
        int trackIndex = -1;
        int count = extractor.getTrackCount();//获取轨道数量
        for (int i = 0; i < count; i++) {
            MediaFormat mediaFormat = extractor.getTrackFormat(i);
            String currentTrack = mediaFormat.getString(MediaFormat.KEY_MIME);
            if (currentTrack.startsWith(targetTrack)) {
                trackIndex = i;
                break;
            }
        }
        return trackIndex;

    }


    private void printVideoMeidaInfo(String path){
        int index = getTrackIndex("video", path);
        if(index < 0){
            return;
        }
        try{
            MediaExtractor extractor = new MediaExtractor();//实例一个MediaExtractor
            extractor.setDataSource(path);
            MediaFormat mediaFormat = extractor.getTrackFormat(index);
            VideoMediaInfo videoMediaInfo = new VideoMediaInfo();
            videoMediaInfo.language = mediaFormat.getString(MediaFormat.KEY_LANGUAGE);//获取语言格式内容
            videoMediaInfo.width = mediaFormat.getInteger(MediaFormat.KEY_WIDTH);
            videoMediaInfo.height = mediaFormat.getInteger(MediaFormat.KEY_HEIGHT);
            videoMediaInfo.durationTime = mediaFormat.getLong(MediaFormat.KEY_DURATION);

            videoMediaInfo.maxByteCount = mediaFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);//获取视频缓存输出的最大大小
            try{
                Integer bitRate = mediaFormat.getInteger(MediaFormat.KEY_BIT_RATE);//获取比特
            }catch (Exception e){
                e.printStackTrace();
            }

            try{
                videoMediaInfo.colorFormat = mediaFormat.getInteger(MediaFormat.KEY_COLOR_FORMAT);//颜色格式
            }catch (Exception e){
                e.printStackTrace();
            }

            try{
                videoMediaInfo.frameRate = mediaFormat.getInteger(MediaFormat.KEY_FRAME_RATE);//帧率

            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                videoMediaInfo. tileWidth = mediaFormat.getInteger(MediaFormat.KEY_TILE_WIDTH);//图块分辨率

            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                videoMediaInfo. tileHeight = mediaFormat.getInteger(MediaFormat.KEY_TILE_HEIGHT);//图块分辨率

            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                videoMediaInfo.gridRows = mediaFormat.getInteger(MediaFormat.KEY_GRID_ROWS);//网格行

            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                videoMediaInfo.gridColumns = mediaFormat.getInteger(MediaFormat.KEY_GRID_COLUMNS);//网格列

            }catch (Exception e){
                e.printStackTrace();
            }

            printLog("printVideoMeidaInfo " + videoMediaInfo);

        }catch (Exception e){
            e.printStackTrace();
        }


    }


    private void printAudioMeidaInfo(String path){
        int index = getTrackIndex("audio", path);
        if(index < 0){
            return;
        }
        try{
            MediaExtractor extractor = new MediaExtractor();//实例一个MediaExtractor
            extractor.setDataSource(path);
            MediaFormat mediaFormat = extractor.getTrackFormat(index);
            AudioMediaInfo mediaINfo = new AudioMediaInfo();
            try{
                 mediaINfo.bitRate = mediaFormat.getInteger(MediaFormat.KEY_BIT_RATE);//获取比特
            }catch (Exception e){
                e.printStackTrace();
            }

            try{
//                mediaINfo.pcmEncoding = mediaFormat.getInteger(MediaFormat.KEY_PCM_ENCODING);//PCM-编码 模拟信号编码
                mediaINfo.sampleRate =  mediaFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE);
                        mediaINfo.channelCount = mediaFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
            }catch (Exception e){
                e.printStackTrace();
            }


            try{
                mediaINfo.isAdts = mediaFormat.getInteger(MediaFormat.KEY_IS_ADTS);
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                mediaINfo.keyChannelMask = mediaFormat.getInteger(MediaFormat.KEY_CHANNEL_MASK);//图块分辨率
            }catch (Exception e){
                e.printStackTrace();
            }
            try{
                mediaINfo.aacProfile = mediaFormat.getInteger(MediaFormat.KEY_AAC_PROFILE);//图块分辨率
            }catch (Exception e){
                e.printStackTrace();
            }


            printLog("printAudioMeidaInfo " + mediaINfo);

        }catch (Exception e){
            e.printStackTrace();
        }


    }



}
package com.yuanxuzhen.androidmedia.demux;

public class VideoMediaInfo {
    public String language; //语言
    public int width; //视频宽
    public int height; //视频高
    public long durationTime;//视频时间
    public int maxByteCount;
    public int colorFormat;
    public int frameRate;
    public int tileWidth;
    public int  tileHeight;
    public int gridRows;
    public int  gridColumns;

    @Override
    public String toString() {
        return "VideoMediaInfo{" +
                "language='" + language + ''' +
                ", width=" + width +
                ", height=" + height +
                ", durationTime=" + durationTime +
                ", maxByteCount=" + maxByteCount +
                ", colorFormat=" + colorFormat +
                ", frameRate=" + frameRate +
                ", tileWidth=" + tileWidth +
                ", tileHeight=" + tileHeight +
                ", gridRows=" + gridRows +
                ", gridColumns=" + gridColumns +
                '}';
    }
}
package com.yuanxuzhen.androidmedia.demux;

public class AudioMediaInfo {
    int bitRate;
    int pcmEncoding;
    int sampleRate;
    int channelCount;
    int isAdts;
    int keyChannelMask;
    int aacProfile;


    @Override
    public String toString() {
        return "AudioMediaInfo{" +
                "bitRate=" + bitRate +
                ", pcmEncoding=" + pcmEncoding +
                ", sampleRate=" + sampleRate +
                ", channelCount=" + channelCount +
                ", isAdts=" + isAdts +
                ", keyChannelMask=" + keyChannelMask +
                ", aacProfile=" + aacProfile +
                '}';
    }
}

gitee地址

https://gitee.com/creat151/android-media.git

最后

以上就是英勇手套为你收集整理的Android MediaExtractor的使用的全部内容,希望文章能够帮你解决Android MediaExtractor的使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部