Access to audio buffer in MediaPlayer

2.5k views Asked by At

I'm working on Android project in which I must analyze audio signals in frequency domain. 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.

Now I'm trying to analyze audio signal which is stored on device as mp3 file. Generally I don't have any issues with playing it from MediaPlayer object. 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?

2

There are 2 answers

3
Geobits On

As far as I know, there is no way to directly access the buffer using a 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. You can perform your analysis on the data, then pass it along to an AudioTrack to output it. 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. I've used JLayer in the past, but its performance is highly dependent on hardware. On some lower-end devices you won't be able to decode in real time.

0
Dave On

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. I mention these for the sake of completeness, as the aforementioned JLayer library may be the simpler solution.

The first is to write a simple server on the device and connect the MediaPlayer to a localhost address. You can process your data before sending it to the 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.

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.