文章目录
前言
从java层到jni,再到framework层,查看format参数的设置,就目前的代码是否支持24bit。
1. java层
Android提供两种类创建音频录制,分布为MediaRecorder和AudioRecord。
两者最直观的差别在于MediaRecorder同时可以录制视频。
1.1 MediaRecorder创建
//0. 创建MediaRecorder实例
mRecorder = new MediaRecorder();
//1. 设置source类型
mRecorder.setAudioSource(audiosourcetype);
//2. 设置三大参数
mRecorder.setAudioChannels(mChannels);
mRecorder.setAudioSamplingRate(mSamplingRate);
mRecorder.setAudioEncodingBitRate(mBitRate);
//3. 设置输出文件类型
mRecorder.setOutputFormat(outputfileformat);
mRecorder.setAudioEncoder(codectype);
//4. 准备
mRecorder.setOutputFile(mSampleFile.getAbsolutePath());
mRecorder.prepare();
//5. 启动
mRecorder.start();
1.2 AudioRecord创建
//1. 创建AudioRecord实例
mRecorder = new AudioRecord(mSelectedRecordSource, mSamplingRate,
mChannelConfig, mAudioFormat,
2 * minRecordBuffSizeInBytes /* double size for padding */);
//2. 从native获取数据
mRecorder.read(mAudioShortArray, 0, mMinRecordBufferSizeInSamples);
这个是参考ctsv的代码,有关线程的就没细看。
需要注意的是,AudioRecord根据入参区分,有三种创建函数。
//1
public AudioRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat,
int bufferSizeInBytes)
//2
public AudioRecord(AudioAttributes attributes, AudioFormat format, int bufferSizeInBytes,
int sessionId) throws IllegalArgumentException {
//3
/*package*/ AudioRecord(long nativeRecordInJavaObj) {
目前主要以第二种为例,该函数会调用到native_setup。
2 JNI层
android_media_MediaRecorder_native_setup
android_media_AudioRecord_setup
3 native层
3.1 MediaRecorder
创建流程
MediaRecorder - AudioSource - AudioRecord
AudioSource 中会创建AudioRecord实例
AudioSource::AudioSource(){
mRecord = new AudioRecord(
inputSource, sampleRate, AUDIO_FORMAT_PCM_16_BIT,
audio_channel_in_mask_from_count(channelCount),
opPackageName,
(size_t) (bufCount * frameCount),
AudioRecordCallbackFunction,
this,
frameCount /*notificationFrames*/,
AUDIO_SESSION_ALLOCATE,
AudioRecord::TRANSFER_DEFAULT,
AUDIO_INPUT_FLAG_NONE,
uid,
pid,
NULL /*pAttributes*/,
selectedDeviceId,
selectedMicDirection,
selectedMicFieldDimension);
}
如上述代码,可以发现AudioRecord的第三个入参为AUDIO_FORMAT_PCM_16_BIT。所以看起来MediaRecorder在native层设置参数的时候强制设置format只支持16bit。
3.2 AudioRecord
android_media_AudioRecord_setup(){
audio_format_t format = audioFormatToNative(audioFormat);
}
//只截取一点
static inline audio_format_t audioFormatToNative(int audioFormat)
{
switch (audioFormat) {
case ENCODING_PCM_16BIT:
return AUDIO_FORMAT_PCM_16_BIT;
case ENCODING_PCM_8BIT:
return AUDIO_FORMAT_PCM_8_BIT;
case ENCODING_PCM_FLOAT:
return AUDIO_FORMAT_PCM_FLOAT;
case ENCODING_AC3:
return AUDIO_FORMAT_AC3;
}
在JNI层,AudioRecord会根据app传下来的format,选择合适的audio_format_t 类型format。
可以看到没有支持24bit的。
app传入的是ENCODING_PCM_16BIT,这个定义在AudioFormat.java中,如果后续想要加入24bit的话估计可以在这里添加,同时还要注意.h文件也要新增。
4 总结
就目前从java层到jni层的代码来看,上层无法传下支持24bit的的format类型。
MediaRecorder是因为在native层强制设置format为AUDIO_FORMAT_PCM_16_BIT。
AudioRecord是因为在JNI层就没有支持24bit的format选项。