I t开发者_JAVA百科ry to record audio using android ndk. people say I can use "frameworks/base/media/libmedia/AudioRecord.cpp". but it is in kernel. how can I access and use it?
The C++ libmedia library is not part of the public API. Some people use it, but this is highly discouraged because it might break on some devices and/or in a future Android release.
I developed an audio recording app, and trust me, audio support is very inconsistent across devices, it's very tricky, so IMO using libmedia directly is a bad idea.
The only way to capture raw audio with the public API is to use the Java AudioRecord class. It will gives you PCM data, which you can then choose to pass to your C optimized routines.
Alternatively, although that's a bit harder, you could write a C/C++ wrapper around the Java AudioRecord class, as it is possible to instantiate Java objects and call methods through JNI.
May be a little-bit outdated but:
the safest way of playing/recording audio in native code is by using OpenSL ES interfaces. Nevertheless it's available only on android 2.3+ and for now works over generic AudioFlinger API.
the more robust and simple way is using platform source-codes to get AudioFlinger headers and some generic libmedia.so used for linking on the build stage. Device-dependent libmedia.so should be preloaded at application initialization stage for AudioFlinger to work normally (generally it is done automatically). Take a note that some vendors try changing AudioFlinger internals (by ambiguous reasons), so you may encounter some memory or behavior issues.
In my experience AudioFlinger worked on all (2.0+) devices but sometimes required allocating more memory for the object than it was supposed by default implementation.
Finally saying OpenSL ES is a wrapper with dynamically loadable C-interface which allows using it with any particular AudioFlinger implementation. It is pretty complicated for simple usage, and may have even more overhead than using Java AudioTrack/AudioRecord because of internal threading, buffering, etc.. So consider using Java or not-so-safe native AudioFlinger until Google implements some high-performance audio interface (which is doubtful for now).
The OpenSL Es API is available from Android 2.3 on (API-Level 9)
精彩评论