How to play MP3 audio buffers

964 views
Skip to first unread message

shah...@essential.com

unread,
Jul 18, 2017, 6:09:27 AM7/18/17
to android-ndk
Hi,

I am developing a C++ only library (no java) using the NDK audio library. One of the functionality I must support is to play MP3 audio buffers (not files, I already done that).
I can't use mediacodec or another open source decoder (just ndk)

This documentation (https://developer.android.com/ndk/guides/audio/opensl-for-android.html) says that I can use the buffer api (Buffer queue data locator) to play PCM data!

How do you go about playing MP3 audio buffers? Can you point me to a documentation or an example code?


Thanks in advance.

Regards, Shahriar

P.S. I know this question has been asked before. But I haven't read a concise answer yet.

Don Turner

unread,
Jul 18, 2017, 6:15:53 AM7/18/17
to android-ndk
The NDK has its own extraction and decoding classes: NdkMediaExtractor and NdkMediaCodec.

Sample code here: https://github.com/googlesamples/android-ndk/blob/master/native-codec

You'll need to modify the sample code to decode MP3 rather than MP4 (video). 

I would advise against using OpenSL ES for MP3 decoding since this technique has some performance issues. 

shah...@essential.com

unread,
Jul 18, 2017, 1:14:39 PM7/18/17
to android-ndk
Hello,

Thanks for the replay. Looking at the header file (NdkMediaExtractor.h) the API supports either a file or url as the source of data (not a buffer):

media_status_t AMediaExtractor_setDataSourceFd(AMediaExtractor*, int fd, off64_t offset, off64_t length);

/**
* Set the URI from which the extractor will read.
*/
media_status_t AMediaExtractor_setDataSource(AMediaExtractor*, const char *location); // TODO support headers


Shahriar

Celina Val

unread,
May 2, 2018, 9:11:26 PM5/2/18
to android-ndk
Hi Shahriar,

Did you ever find the solution for your problem? I have the same problem. The API that Don mentioned is only available for the beta ndk-17 and only available for API Level 28+. :(

Thanks,
Celina
Message has been deleted

Don Turner

unread,
Jun 5, 2018, 10:16:02 AM6/5/18
to android-ndk
I'm pretty sure the NDKMediaCodec API is available down to API 16 (if not earlier). It's definitely been around since 2014 https://android.googlesource.com/platform/frameworks/av/+/0c3be875376adaee8d8e8dd917c64926e1513b29/include/ndk/NdkMediaCodec.h

Further to this, I would recommend looking at FFmpeg for the best possible performance. I'm currently working on a sample and documentation for this but rest assured it can be done and provides better performance than the platform APIs. 

Celina Val

unread,
Jun 5, 2018, 1:10:24 PM6/5/18
to andro...@googlegroups.com
Hi Don,

Thanks for replying. I believe we were talking about the media extractor API. Extracting the format from a buffer is only available in newer versions.

That said, I decided to use FFmpeg and it works perfectly fine.

Thanks,
Celina

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/0753c187-1d0f-4922-bddd-50a2d29e2711%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Don Turner

unread,
Jun 5, 2018, 1:14:15 PM6/5/18
to android-ndk
Ah I see, thanks for clarifying. Glad things worked out with FFmpeg. 

Could I ask, is this an open source project you're working on? If so, is the source available somewhere? 
Celina

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages