简体   繁体   English

访问MediaPlayer中的音频缓冲区

[英]Access to audio buffer in MediaPlayer

I'm working on Android project in which I must analyze audio signals in frequency domain. 我正在研究Android项目,我必须在频域中分析音频信号。 Actually I have ready module which is reading signal from device microphone and process it (FFT, visualizations, etc.) and I don't have any problems with this kind of signals. 实际上我有准备好的模块,它是从设备麦克风读取信号并处理它(FFT,可视化等),我对这种信号没有任何问题。

Now I'm trying to analyze audio signal which is stored on device as mp3 file. 现在我正在尝试分析存储在设备上的音频信号作为mp3文件。 Generally I don't have any issues with playing it from MediaPlayer object. 一般来说,从MediaPlayer对象播放它没有任何问题。 Unfortunately I have some problem with getting access to data buffer played by my player, which I could you use to spectrum calculations. 不幸的是我访问我的播放器播放的数据缓冲区时遇到了一些问题,我可以将其用于频谱计算。

Do you have any advices how can I get access to audio buffer? 您有什么建议我如何才能访问音频缓冲区? Is it at all possible with MediaPlayer or should I use other approach to this problem? 是否可以使用MediaPlayer或我应该使用其他方法来解决这个问题?

As far as I know, there is no way to directly access the buffer using a MediaPlayer . 据我所知,没有办法使用MediaPlayer直接访问缓冲区。

However, if you are using API >= 16 (Android 4.1), you can use the built-in MediaCodec class to decode your MP3 file to a raw PCM stream. 但是,如果您使用的是API> = 16(Android 4.1),则可以使用内置的MediaCodec类将MP3文件解码为原始PCM流。 You can perform your analysis on the data, then pass it along to an AudioTrack to output it. 您可以对数据执行分析,然后将其传递给AudioTrack以输出数据。 This blog post shows an example of how to decode and send to an audio track. 此博客文章显示了如何解码和发送到音频轨道的示例。

If you must support lower API levels, you can use a 3rd party library the same way. 如果必须支持较低的API级别,则可以使用相同的方式使用第三方库。 I've used JLayer in the past, but its performance is highly dependent on hardware. 我过去曾使用过JLayer ,但其性能高度依赖于硬件。 On some lower-end devices you won't be able to decode in real time. 在某些低端设备上,您将无法实时解码。

Generic Holiday Name's answer is correct that there's no way to access the buffers of the MediaPlayer , but there are other options that don't require a 3rd party library. Generic Holiday Name的答案是正确的,没有办法访问MediaPlayer的缓冲区,但还有其他选项不需要第三方库。 I mention these for the sake of completeness, as the aforementioned JLayer library may be the simpler solution. 我提到这些是为了完整,因为前面提到的JLayer库可能是更简单的解决方案。

The first is to write a simple server on the device and connect the MediaPlayer to a localhost address. 第一种是在设备上编写一个简单的服务器,并将MediaPlayer连接到本地主机地址。 You can process your data before sending it to the MediaPlayer . 您可以在将数据发送到MediaPlayer之前处理数据。 The downside of this approach is that you would need some way to decode the data from mp3 to PCM to be generally usable. 这种方法的缺点是你需要一些方法来解码从mp3到PCM的数据,以便通常可用。

The second alternative is to use native code and OpenSL, although support for it only goes back to API 9. Building a simple player is relatively straightforward, and you could easily gain direct access to PCM data. 第二种方法是使用本机代码和OpenSL,尽管对它的支持只能回到API 9.构建一个简单的播放器相对简单,您可以轻松获得对PCM数据的直接访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM