如何检测是否支持MediaRecorder.AudioEncoder.HE_AAC?

时间:2019-02-19 15:41:35

标签: android

我希望尽可能使用MediaRecorder.AudioEncoder.HE_AAC

但是,除非我使用MediaRecorder.AudioEncoder.AAC,否则以下代码无法在某些设备中生成有效的音频文件。

private void startMediaRecorder() {
    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setAudioChannels(1);
    mediaRecorder.setAudioSamplingRate(8000);
    mediaRecorder.setAudioEncodingBitRate(32000);
    /*
    mediaRecorder.setAudioChannels(1);
    mediaRecorder.setAudioSamplingRate(44100);
    mediaRecorder.setAudioEncodingBitRate(96000);
     */
    mediaRecorder.setMaxDuration(RECORDING_MEDIA_RECORDER_MAX_DURATION);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
    } else {
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    }
    mediaRecorder.setOutputFile(getMicFilepath());

    mediaRecorder.setOnInfoListener((mediaRecorder, what, i1) -> {
        if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
            dismiss();
        }
    });

    try {
        mediaRecorder.prepare();
    } catch (IOException e) {
        Log.e(TAG, "", e);
    }

    mediaRecorder.start();
}

对于不支持HE_AAC的设备,不会崩溃也不会引发异常。但是,您可以看到以下错误日志

  

E / MediaProfiles:找不到给定的音频编码器4


我尝试使用MediaCodecList.getCodecInfoAt来查看它是否可以为我提供一些有用的信息,有关所选设备是否支持MediaRecorder.AudioEncoder.HE_AAC

    int numCodecs = 0;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        numCodecs = MediaCodecList.getCodecCount();

        for (int i = 0; i < numCodecs; i++) {
            MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);

            if (!codecInfo.isEncoder()) {
                continue;
            }

            for (String s : codecInfo.getSupportedTypes())
                android.util.Log.i("CHEOK", "getSupportedTypes = " + s);
            android.util.Log.i("CHEOK", "name = " + codecInfo.getName());
        }
    }

不支持HE_AAC的设备的输出

getSupportedTypes = audio/mp4a-latm
name = OMX.google.aac.encoder
getSupportedTypes = audio/3gpp
name = OMX.google.amrnb.encoder
getSupportedTypes = audio/amr-wb
name = OMX.google.amrwb.encoder
getSupportedTypes = audio/flac
name = OMX.google.flac.encoder
getSupportedTypes = video/3gpp
name = OMX.google.h263.encoder
getSupportedTypes = video/avc
name = OMX.google.h264.encoder
getSupportedTypes = video/mp4v-es
name = OMX.google.mpeg4.encoder
getSupportedTypes = video/x-vnd.on2.vp8
name = OMX.google.vp8.encoder

支持HE_AAC的设备的输出

getSupportedTypes = audio/mp4a-latm
name = OMX.google.aac.encoder
getSupportedTypes = audio/3gpp
name = OMX.google.amrnb.encoder
getSupportedTypes = audio/amr-wb
name = OMX.google.amrwb.encoder
getSupportedTypes = audio/flac
name = OMX.google.flac.encoder
getSupportedTypes = video/avc
name = OMX.qcom.video.encoder.avc
getSupportedTypes = video/avc
name = OMX.google.h264.encoder
getSupportedTypes = video/3gpp
name = OMX.qcom.video.encoder.h263
getSupportedTypes = video/3gpp
name = OMX.google.h263.encoder
getSupportedTypes = video/hevc
name = OMX.qcom.video.encoder.hevc
getSupportedTypes = video/mp4v-es
name = OMX.qcom.video.encoder.mpeg4
getSupportedTypes = video/mp4v-es
name = OMX.google.mpeg4.encoder
getSupportedTypes = video/x-vnd.on2.vp8
name = OMX.qcom.video.encoder.vp8
getSupportedTypes = video/x-vnd.on2.vp8
name = OMX.google.vp8.encoder
getSupportedTypes = video/x-vnd.on2.vp9
name = OMX.google.vp9.encoder

基于上面的输出,我几乎无法将MediaCodecList.getCodecInfoAt返回的信息与是否支持MediaRecorder.AudioEncoder.HE_AAC相关联。

关于什么是可靠方法的任何建议?


更新的解决方案

这是解决方案,假设可以使用配置文件MediaCodecInfo.CodecProfileLevel.AACObjectHEMediaCodecInfo.CodecProfileLevel.AACObjectHE_PS检测到HE_AAC

我会错过其他个人资料吗?

private static final String MIMETYPE_AUDIO_AAC = "audio/mp4a-latm";

private boolean isHeAacEncoderSupported() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        int numCodecs = MediaCodecList.getCodecCount();

        for (int i = 0; i < numCodecs; i++) {
            MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);

            if (!codecInfo.isEncoder()) {
                continue;
            }

            for (String supportedType : codecInfo.getSupportedTypes()) {
                if (supportedType.equalsIgnoreCase(MIMETYPE_AUDIO_AAC)) {
                    MediaCodecInfo.CodecCapabilities cap = codecInfo.getCapabilitiesForType(MIMETYPE_AUDIO_AAC);
                    MediaCodecInfo.CodecProfileLevel[] profileLevels = cap.profileLevels;
                    for (MediaCodecInfo.CodecProfileLevel profileLevel : profileLevels) {
                        int profile = profileLevel.profile;
                        if (profile == MediaCodecInfo.CodecProfileLevel.AACObjectHE || profile == MediaCodecInfo.CodecProfileLevel.AACObjectHE_PS) {
                            return true;
                        }
                    }
                }
            }
        }
    }

    return false;
}

1 个答案:

答案 0 :(得分:1)

所有AAC编解码器共享相同的MIME类型,但是您应该能够通过profile level来区分它们,并与MediaCodecInfo.CodecProfileLevel进行比较。