本文主要是介绍使用VOICE_CALL在Android5.0之后闪退bug源码解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
android4.4_r1版本AndroidRecord.java中源码如下
public More ...AudioRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat,
209 int bufferSizeInBytes)
210 throws IllegalArgumentException {
211 mRecordingState = RECORDSTATE_STOPPED;
212
213 // remember which looper is associated with the AudioRecord instanciation
214 if ((mInitializationLooper = Looper.myLooper()) == null) {
215 mInitializationLooper = Looper.getMainLooper();
216 }
217
218 audioParamCheck(audioSource, sampleRateInHz, channelConfig, audioFormat);
219
220 audioBuffSizeCheck(bufferSizeInBytes);
221
222 // native initialization
223 int[] session = new int[1];
224 session[0] = 0;
225 //TODO: update native initialization when information about hardware init failure
226 // due to capture device already open is available.
227 int initResult = native_setup( new WeakReference<AudioRecord>(this),
228 mRecordSource, mSampleRate, mChannelMask, mAudioFormat, mNativeBufferSizeInBytes,
229 session);
230 if (initResult != SUCCESS) {
231 loge("Error code "+initResult+" when initializing native AudioRecord object.");
232 return; // with mState == STATE_UNINITIALIZED
233 }
234
235 mSessionId = session[0];
236
237 mState = STATE_INITIALIZED;
238 }
239
240
241 // Convenience method for the constructor's parameter checks.
242 // This is where constructor IllegalArgumentException-s are thrown
243 // postconditions:
244 // mRecordSource is valid
245 // mChannelCount is valid
246 // mChannelMask is valid
247 // mAudioFormat is valid
248 // mSampleRate is valid
249 private void More ...audioParamCheck(int audioSource, int sampleRateInHz,
250 int channelConfig, int audioFormat) {
251
252 //--------------
253 // audio source
254 if ( (audioSource < MediaRecorder.AudioSource.DEFAULT) ||
255 ((audioSource > MediaRecorder.getAudioSourceMax()) &&
256 (audioSource != MediaRecorder.AudioSource.HOTWORD)) ) {
257 throw new IllegalArgumentException("Invalid audio source.");
258 }
259 mRecordSource = audioSource;
260
261 //--------------
262 // sample rate
263 if ( (sampleRateInHz < 4000) || (sampleRateInHz > 48000) ) {
264 throw new IllegalArgumentException(sampleRateInHz
265 + "Hz is not a supported sample rate.");
266 }
267 mSampleRate = sampleRateInHz;
268
269 //--------------
270 // channel config
271 switch (channelConfig) {
272 case AudioFormat.CHANNEL_IN_DEFAULT: // AudioFormat.CHANNEL_CONFIGURATION_DEFAULT
273 case AudioFormat.CHANNEL_IN_MONO:
274 case AudioFormat.CHANNEL_CONFIGURATION_MONO:
275 mChannelCount = 1;
276 mChannelMask = AudioFormat.CHANNEL_IN_MONO;
277 break;
278 case AudioFormat.CHANNEL_IN_STEREO:
279 case AudioFormat.CHANNEL_CONFIGURATION_STEREO:
280 mChannelCount = 2;
281 mChannelMask = AudioFormat.CHANNEL_IN_STEREO;
282 break;
283 case (AudioFormat.CHANNEL_IN_FRONT | AudioFormat.CHANNEL_IN_BACK):
284 mChannelCount = 2;
285 mChannelMask = channelConfig;
286 break;
287 default:
288 throw new IllegalArgumentException("Unsupported channel configuration.");
289 }
290
291 //--------------
292 // audio format
293 switch (audioFormat) {
294 case AudioFormat.ENCODING_DEFAULT:
295 mAudioFormat = AudioFormat.ENCODING_PCM_16BIT;
296 break;
297 case AudioFormat.ENCODING_PCM_16BIT:
298 case AudioFormat.ENCODING_PCM_8BIT:
299 mAudioFormat = audioFormat;
300 break;
301 default:
302 throw new IllegalArgumentException("Unsupported sample encoding."
303 + " Should be ENCODING_PCM_8BIT or ENCODING_PCM_16BIT.");
304 }
305 }
306
307
308 // Convenience method for the contructor's audio buffer size check.
309 // preconditions:
310 // mChannelCount is valid
311 // mAudioFormat is AudioFormat.ENCODING_PCM_8BIT OR AudioFormat.ENCODING_PCM_16BIT
312 // postcondition:
313 // mNativeBufferSizeInBytes is valid (multiple of frame size, positive)
314 private void More ...audioBuffSizeCheck(int audioBufferSize) {
315 // NB: this section is only valid with PCM data.
316 // To update when supporting compressed formats
317 int frameSizeInBytes = mChannelCount
318 * (mAudioFormat == AudioFormat.ENCODING_PCM_8BIT ? 1 : 2);
319 if ((audioBufferSize % frameSizeInBytes != 0) || (audioBufferSize < 1)) {
320 throw new IllegalArgumentException("Invalid audio buffer size.");
321 }
322
323 mNativeBufferSizeInBytes = audioBufferSize;
324 }
可以看到其中对audioSource的校验部分并没有做区分,所以理论上可以使用所有音频源
Android5.0.0_r1版本时,代码更新如下
222
223 public More ...AudioRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat,
224 int bufferSizeInBytes)
225 throws IllegalArgumentException {
226 this((new AudioAttributes.Builder())
227 .setInternalCapturePreset(audioSource)
228 .build(),
229 (new AudioFormat.Builder())
230 .setChannelMask(getChannelMaskFromLegacyConfig(channelConfig,
231 true/*allow legacy configurations*/))
232 .setEncoding(audioFormat)
233 .setSampleRate(sampleRateInHz)
234 .build(),
235 bufferSizeInBytes,
236 AudioManager.AUDIO_SESSION_ID_GENERATE);
237 }
并且增加了如下系统级初始化方法
Parameters:
attributes a non-null AudioAttributes instance. Use AudioAttributes.Builder.setCapturePreset(int) for configuring the capture preset for this instance.
format a non-null AudioFormat instance describing the format of the data that will be recorded through this AudioRecord. See AudioFormat.Builder for configuring the audio format parameters such as encoding, channel mask and sample rate.
bufferSizeInBytes the total size (in bytes) of the buffer where audio data is written to during the recording. New audio data can be read from this buffer in smaller chunks than this size. See getMinBufferSize(int,int,int) to determine the minimum required buffer size for the successful creation of an AudioRecord instance. Using values smaller than getMinBufferSize() will result in an initialization failure.
sessionId ID of audio session the AudioRecord must be attached to, or AudioManager.AUDIO_SESSION_ID_GENERATE if the session isn't known at construction time. See also AudioManager.generateAudioSessionId() to obtain a session ID before construction.
Throws:
java.lang.IllegalArgumentException
Hide:
CANDIDATE FOR PUBLIC API Class constructor with AudioAttributes and AudioFormat.
259
260 public More ...AudioRecord(AudioAttributes attributes, AudioFormat format, int bufferSizeInBytes,
261 int sessionId) throws IllegalArgumentException {
262 mRecordingState = RECORDSTATE_STOPPED;
263
264 if (attributes == null) {
265 throw new IllegalArgumentException("Illegal null AudioAttributes");
266 }
267 if (format == null) {
268 throw new IllegalArgumentException("Illegal null AudioFormat");
269 }
270
271 // remember which looper is associated with the AudioRecord instanciation
272 if ((mInitializationLooper = Looper.myLooper()) == null) {
273 mInitializationLooper = Looper.getMainLooper();
274 }
275
276 mAudioAttributes = attributes;
277
278 // is this AudioRecord using REMOTE_SUBMIX at full volume?
279 if (mAudioAttributes.getCapturePreset() == MediaRecorder.AudioSource.REMOTE_SUBMIX) {
280 final Iterator<String> tagsIter = mAudioAttributes.getTags().iterator();
281 while (tagsIter.hasNext()) {
282 if (tagsIter.next().equalsIgnoreCase(SUBMIX_FIXED_VOLUME)) {
283 mIsSubmixFullVolume = true;
284 Log.v(TAG, "Will record from REMOTE_SUBMIX at full fixed volume");
285 break;
286 }
287 }
288 }
289
290 int rate = 0;
291 if ((format.getPropertySetMask()
292 & AudioFormat.AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE) != 0)
293 {
294 rate = format.getSampleRate();
295 } else {
296 rate = AudioSystem.getPrimaryOutputSamplingRate();
297 if (rate <= 0) {
298 rate = 44100;
299 }
300 }
301
302 int encoding = AudioFormat.ENCODING_DEFAULT;
303 if ((format.getPropertySetMask() & AudioFormat.AUDIO_FORMAT_HAS_PROPERTY_ENCODING) != 0)
304 {
305 encoding = format.getEncoding();
306 }
307
308 audioParamCheck(attributes.getCapturePreset(), rate, encoding);
309
310 mChannelCount = AudioFormat.channelCountFromInChannelMask(format.getChannelMask());
311 mChannelMask = getChannelMaskFromLegacyConfig(format.getChannelMask(), false);
312
313 audioBuffSizeCheck(bufferSizeInBytes);
314
315 int[] session = new int[1];
316 session[0] = sessionId;
317 //TODO: update native initialization when information about hardware init failure
318 // due to capture device already open is available.
319 int initResult = native_setup( new WeakReference<AudioRecord>(this),
320 mAudioAttributes, mSampleRate, mChannelMask, mAudioFormat, mNativeBufferSizeInBytes,
321 session);
322 if (initResult != SUCCESS) {
323 loge("Error code "+initResult+" when initializing native AudioRecord object.");
324 return; // with mState == STATE_UNINITIALIZED
325 }
326
327 mSessionId = session[0];
328
329 mState = STATE_INITIALIZED;
330 }
跟踪对音频源的处理入口可以查到AudioAttributes.java中
Parameters:
preset
Returns:
the same Builder instance.
Hide:
Same as setCapturePreset(int) but authorizes the use of HOTWORD and REMOTE_SUBMIX.
525
526 public Builder More ...setInternalCapturePreset(int preset) {
527 if ((preset == MediaRecorder.AudioSource.HOTWORD)
528 || (preset == MediaRecorder.AudioSource.REMOTE_SUBMIX)) {
529 mSource = preset;
530 } else {
531 setCapturePreset(preset);
532 }
533 return this;
534 }
535 };
536
537 @Override
538 public int More ...describeContents() {
539 return 0;
540 }
以及
Parameters:
preset one of MediaRecorder.AudioSource.DEFAULT, MediaRecorder.AudioSource.MIC, MediaRecorder.AudioSource.CAMCORDER, MediaRecorder.AudioSource.VOICE_RECOGNITION or MediaRecorder.AudioSource.VOICE_COMMUNICATION.
Returns:
the same Builder instance.
Hide:
Sets the capture preset. Use this audio attributes configuration method when building an AudioRecord instance with AudioRecord.(android.media.AudioAttributes,android.media.AudioFormat,int).
503
504 public Builder More ...setCapturePreset(int preset) {
505 switch (preset) {
506 case MediaRecorder.AudioSource.DEFAULT:
507 case MediaRecorder.AudioSource.MIC:
508 case MediaRecorder.AudioSource.CAMCORDER:
509 case MediaRecorder.AudioSource.VOICE_RECOGNITION:
510 case MediaRecorder.AudioSource.VOICE_COMMUNICATION:
511 mSource = preset;
512 break;
513 default:
514 Log.e(TAG, "Invalid capture preset " + preset + " for AudioAttributes");
515 }
516 return this;
517 }
可以发现VOICE_CALL音频源已经不能使用,所以抛出Invalid capture preset异常
从代码中并不能发现为何MIC这个音频源为何在高版本无法录到通话对方的声音
这篇关于使用VOICE_CALL在Android5.0之后闪退bug源码解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!