轉載請標明原創:https://blog.csdn.net/qq_29621351/article/details/94562600
在Android應用程序中採用NDK原生的OpenSLES進行聲音的播放是一個不錯的選擇(由於在OpenSLES是在C++層播放,跟硬件比較接近所以效率高,採集和播放的延遲也比Android組件延遲小得多),如果說有什麼缺點,就是接口比較複雜,而且只能播放原始音頻數據(PCM格式)。
在NDK的 OpenSLES_AndroidConfiguration.h 頭文件中包含了使用OpenSLES進行採集和渲染的預設選項,這些選項是Android針對具體的業務類型加入的一些採集和渲染上的優化,選項如下
/*---------------------------------------------------------------------------*/
/* Android AudioRecorder configuration */
/*---------------------------------------------------------------------------*/
/** Audio recording preset */
/** Audio recording preset key */
#define SL_ANDROID_KEY_RECORDING_PRESET ((const SLchar*) "androidRecordingPreset")
/** Audio recording preset values */
/** preset "none" cannot be set, it is used to indicate the current settings
* do not match any of the presets. */
#define SL_ANDROID_RECORDING_PRESET_NONE ((SLuint32) 0x00000000)
/** generic recording configuration on the platform */
#define SL_ANDROID_RECORDING_PRESET_GENERIC ((SLuint32) 0x00000001)
/** uses the microphone audio source with the same orientation as the camera
* if available, the main device microphone otherwise */
#define SL_ANDROID_RECORDING_PRESET_CAMCORDER ((SLuint32) 0x00000002)
/** uses the main microphone tuned for voice recognition */
#define SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION ((SLuint32) 0x00000003)
/** uses the main microphone tuned for audio communications */
#define SL_ANDROID_RECORDING_PRESET_VOICE_COMMUNICATION ((SLuint32) 0x00000004)
/** uses the main microphone unprocessed */
#define SL_ANDROID_RECORDING_PRESET_UNPROCESSED ((SLuint32) 0x00000005)
/*---------------------------------------------------------------------------*/
/* Android AudioPlayer configuration */
/*---------------------------------------------------------------------------*/
/** Audio playback stream type */
/** Audio playback stream type key */
#define SL_ANDROID_KEY_STREAM_TYPE ((const SLchar*) "androidPlaybackStreamType")
/** Audio playback stream type values */
/* same as android.media.AudioManager.STREAM_VOICE_CALL */
#define SL_ANDROID_STREAM_VOICE ((SLint32) 0x00000000)
/* same as android.media.AudioManager.STREAM_SYSTEM */
#define SL_ANDROID_STREAM_SYSTEM ((SLint32) 0x00000001)
/* same as android.media.AudioManager.STREAM_RING */
#define SL_ANDROID_STREAM_RING ((SLint32) 0x00000002)
/* same as android.media.AudioManager.STREAM_MUSIC */
#define SL_ANDROID_STREAM_MEDIA ((SLint32) 0x00000003)
/* same as android.media.AudioManager.STREAM_ALARM */
#define SL_ANDROID_STREAM_ALARM ((SLint32) 0x00000004)
/* same as android.media.AudioManager.STREAM_NOTIFICATION */
#define SL_ANDROID_STREAM_NOTIFICATION ((SLint32) 0x00000005)
/*---------------------------------------------------------------------------*/
/* Android AudioPlayer and AudioRecorder configuration */
/*---------------------------------------------------------------------------*/
/** Audio Performance mode.
* Performance mode tells the framework how to configure the audio path
* for a player or recorder according to application performance and
* functional requirements.
* It affects the output or input latency based on acceptable tradeoffs on
* battery drain and use of pre or post processing effects.
* Performance mode should be set before realizing the object and should be
* read after realizing the object to check if the requested mode could be
* granted or not.
*/
/** Audio Performance mode key */
#define SL_ANDROID_KEY_PERFORMANCE_MODE ((const SLchar*) "androidPerformanceMode")
/** Audio performance values */
/* No specific performance requirement. Allows HW and SW pre/post processing. */
#define SL_ANDROID_PERFORMANCE_NONE ((SLuint32) 0x00000000)
/* Priority given to latency. No HW or software pre/post processing.
* This is the default if no performance mode is specified. */
#define SL_ANDROID_PERFORMANCE_LATENCY ((SLuint32) 0x00000001)
/* Priority given to latency while still allowing HW pre and post processing. */
#define SL_ANDROID_PERFORMANCE_LATENCY_EFFECTS ((SLuint32) 0x00000002)
/* Priority given to power saving if latency is not a concern.
* Allows HW and SW pre/post processing. */
#define SL_ANDROID_PERFORMANCE_POWER_SAVING ((SLuint32) 0x00000003)
在採集選項中包含
xxx_RECORDING_PRESET_GENERIC(通用配置,不知道是啥意思)
xxx_RECORDING_PRESET_CAMCORDER(錄像中優先使用攝像頭同方向的Mic,如果沒有同方向的就使用主Mic)
xxx_RECORDING_PRESET_VOICE_RECOGNITION(針對語音識別業務進行了優化,可能使用降噪Mic)
xxx_RECORDING_PRESET_VOICE_COMMUNICATION(針對電話或網絡電話優化,可能會硬件AEC、NS、AGC)
xxx_RECORDING_PRESET_UNPROCESSED(使用主Mic採集,不經過任何優化處理)
在渲染選項中包含
xxx_STREAM_VOICE(VoIP或者電話,音量需要通過通話音量調節)
xxx_STREAM_SYSTEM(系統音量,我的華爲P10沒有這個音量選項)
xxx_STREAM_RING(鈴聲音量)
xxx_STREAM_MEDIA(媒體音量)
xxx_STREAM_ALARM(鬧鐘音量)
下面這個選項沒用過,看字面意思大概表示採集和渲染的過程中對於延遲和省電的權衡,決定哪個更重要,以後用到再補充。