Android AudioRecord 和 AudioTrack 编解码器选项?

发布于 2024-08-19 18:49:33 字数 1753 浏览 10 评论 0原文

我目前在 Android 中使用 AudioTrack 和 AudioRecord 类。

我使用纯 PCM 数据,但我想知道其他编解码器的选项是什么?

来自此页面看来我只能使用 AMR 窄带进行编码和解码?

我当前设置音频类如下:

arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
                     11025,
                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                     AudioFormat.ENCODING_PCM_16BIT,
                     buffersize);

atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                     11025,
                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                     AudioFormat.ENCODING_PCM_16BIT,
                     buffersize,
                     AudioTrack.MODE_STREAM);

所以我的问题是如何将编码从 PCM 更改为其他受支持的编解码器之一?

当我尝试更改 AudioFormat 上的 ENCODING_PCM_16BIT 时,我只获得默认或无效编码的选项以及 PCM 选项。

如果有人知道这里的任何帮助,那么任何关于 Android 上音频编码和解码教程的链接都会很棒,非常感谢。

谢谢

编辑:我已将代码更改为以下内容:

arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
                     11025,
                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                     **MediaRecorder.AudioEncoder.AMR_NB**,
                     buffersize);

atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                     11025,
                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                     **MediaRecorder.AudioEncoder.AMR_NB**,
                     buffersize,
                     AudioTrack.MODE_STREAM);

代码运行正常,但我想知道它是否实际上将音频编码为 AMR_NB 以及这是否不是正确的方法?

我在使用原始 PCM 时遇到缓冲区溢出问题,但自从使用新代码并使用 MediaRecorder.AudioEncoder.AMR_NB 而不是 AudioFormat.PCM 后,没有出现缓冲区溢出问题

I currently use the AudioTrack and AudioRecord classes in Android.

I use the pure PCM data but I was wondering what my options are for other codecs?

From this page it seems I can only encode and decode using AMR narrowband?

I currently set up the Audio classes as follows:

arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
                     11025,
                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                     AudioFormat.ENCODING_PCM_16BIT,
                     buffersize);

atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                     11025,
                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                     AudioFormat.ENCODING_PCM_16BIT,
                     buffersize,
                     AudioTrack.MODE_STREAM);

So my question is how do I change the encoding from PCM to one of the other supported codecs?

When I try to change ENCODING_PCM_16BIT on AudioFormat I only get the options of default or invalid encoding along with the PCM options.

Any links to tutorials on encoding and decoding audio on Android would be great if anyone knows of any or any help here greatly appreciated.

Thanks

EDIT: I have changed my code to the following:

arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
                     11025,
                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                     **MediaRecorder.AudioEncoder.AMR_NB**,
                     buffersize);

atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                     11025,
                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                     **MediaRecorder.AudioEncoder.AMR_NB**,
                     buffersize,
                     AudioTrack.MODE_STREAM);

The code runs properly but I'm wondering does it actually encode the Audio as AMR_NB and if this is not a proper way to do it?

I was getting a buffer overflow when using raw PCM but none have appeared since using the new code with the MediaRecorder.AudioEncoder.AMR_NB used instead of the AudioFormat.PCM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2024-08-26 18:49:33

正如 AudioRecordAudioTrack 的文档所述:

audioFormat     the format in which the audio data is represented. See ENCODING_PCM_16BIT and ENCODING_PCM_8BIT

您只能使用 8 位和 16 位 PCM。如果您想要其他格式的音频,请不要使用 AudioRecordAudioTrack(尝试 MediaRecorderMediaPlayer)或者您必须使用自己的代码(可能利用 NDK)对其进行转码。

AudioRecordAudioTrack 专为 OpenCORE 多媒体引擎不支持相关音频的情况而设计,因为它不是受支持的编解码器或不受支持的流协议(例如,SIP)。

As the documentation states for AudioRecord and AudioTrack:

audioFormat     the format in which the audio data is represented. See ENCODING_PCM_16BIT and ENCODING_PCM_8BIT

you can only work with 8-bit and 16-bit PCM. If you want audio in other formats, either don't use AudioRecord and AudioTrack (try MediaRecorder and MediaPlayer) or you will have to transcode it using your own code, possibly leveraging the NDK.

AudioRecord and AudioTrack are designed specifically for cases where the audio in question is not supported by the OpenCORE multimedia engine, either because it's not a supported codec or not a supported streaming protocol (e.g., SIP).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文