java 适用于 Android 的低延迟音频 API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1448630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:34:14  来源:igfitidea点击:

Low latency audio api for Android?

javaandroidaudiomobile

提问by Rich

What are my options for playing simultaneous audio on an Android device with the least amount of latency? Am I going to get anything half decent out of the canned SDK, or is that asking too much? The documentation claims that the SoundPool class is capable of playing multiple sounds simultaneously with relatively good performance, but after running some tests in the emulator and on a physical device it seems pretty weak. Is there maybe a trick to it, or do I have to go to a much lower level api for this kind of thing? I've tried using a single sound pool with multiple samples loaded, and I've tried multiple sound pools each managing a single sample. I'm preloading everything, so that when I attempt to play back I have no additional code being executed other than the call to SoundPool.play().

我有哪些选项可以以最少的延迟在 Android 设备上同时播放音频?我会从罐装 SDK 中得到任何半体面的东西,还是要求太多?该文档声称 SoundPool 类能够以相对较好的性能同时播放多个声音,但在模拟器和物理设备上运行一些测试后,它似乎很弱。可能有什么技巧,还是我必须为这种事情使用低得多的api?我尝试使用加载了多个样本的单个声音池,并且尝试了多个声音池,每个声音池管理一个样本。我正在预加载所有内容,因此当我尝试播放时,除了对 SoundPool.play() 的调用之外,没有其他代码正在执行。

回答by mattbh

A number of people are interested in low-latency audio on Andriod. Here are the threads I'm following on the topic:

许多人对 Andriod 上的低延迟音频感兴趣。以下是我关注的主题:

  • A wavecovering audio related topics from this year's I/O conference.
  • An Android issueabout support for low latency audio in the NDK.
  • 一个覆盖音频相关主题从今年的I / O大会。
  • 关于在 NDK 中支持低延迟音频的 Android问题

This postsuggests that Android devices have ALSA drivers (capable of low-latency audio)--but it seems that low-latency functionality isn't exposed to apps via the NDK.

这篇文章表明 Android 设备具有 ALSA 驱动程序(能够提供低延迟音频)——但似乎低延迟功能没有通过 NDK 公开给应用程序。

I have no direct experience with Android, but from what I've read, low-latency (less than <10ms or so) isn't a reality yet. Please post any experience to the contrary!

我没有直接使用 Android 的经验,但从我读过的内容来看,低延迟(小于 10 毫秒左右)还不是现实。请发表任何相反的经验!

回答by dljava

Android 2.3 now supports native access to audio APIs (via OpenSL) for low latency applications.

Android 2.3 现在支持对低延迟应用程序的音频 API(通过 OpenSL)进行本机访问。

However, not all hardware devices will have the a low latency audio feature profile. Thus applications requiring low latency audio shouuld filter devices not supporting it in the Android market by specifying the following in the manifest:

但是,并非所有硬件设备都具有低延迟音频功能配置文件。因此,需要低延迟音频的应用程序应该通过在清单中指定以下内容来过滤 Android 市场中不支持它的设备:

<uses-feature android:name="android.hardware.audio.low_latency"/>

回答by MusiGenesis

I don't have any Android experience, but I've written similar things for Windows Mobile. The devices themselves are certainly capable of mixing multiple sounds in real-time with a low latency (under 25 ms), although by "multiple" I mean maybe 4 or 5 (and not 30 to 40). However, I was only able to achieve this satisfactorily by writing my own code that did the mixing internally and accessed the low-level audio playback API only for playing the final mixed output. The higher-level ways of playing sounds in the .Net Compact Framework are theoretically capable of polyphony, but in practice they work horribly (lots of glitches, stuttering and distortion).

我没有任何 Android 经验,但我为 Windows Mobile 编写了类似的东西。设备本身当然能够以低延迟(低于 25 毫秒)实时混合多个声音,尽管“多个”我的意思可能是 4 或 5 个(而不是 30 到 40 个)。但是,我只能通过编写自己的代码来令人满意地实现这一点,该代码在内部进行混音并访问低级音频播放 API 仅用于播放最终的混音输出。在 .Net Compact Framework 中播放声音的高级方法理论上能够进行复音,但实际上它们工作得非常糟糕(很多小故障、口吃和失真)。

I suspect that the Android audio SDK has the same problem, so you may have to write your own.

我怀疑 Android 音频 SDK 也有同样的问题,因此您可能需要自己编写。

回答by Ian Ni-Lewis

Please see my answer to Android: sound API (deterministic, low latency).

请参阅我对Android 的回答: sound API (deterministic, low delay)

The latency of an Android device depends less on the API and more on the hardware and drivers for that particular device. Enabling low latency playback consumes more power and increases the chances of audio glitches, so many OEMs will enlarge their playback buffers intentionally.

Android 设备的延迟较少取决于 API,而更多地取决于该特定设备的硬件和驱动程序。启用低延迟播放会消耗更多电量并增加出现音频故障的可能性,因此许多 OEM 会有意扩大其播放缓冲区。