gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:55:55 25 4
gpt4 key购买 nike

我希望尽可能使用 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: The given audio encoder 4 is not found


我尝试了 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;
}

最佳答案

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

关于android - 如何检测是否支持MediaRecorder.AudioEncoder.HE_AAC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54770010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com