使用VOICE_CALL在Android5.0之后闪退bug源码解析

2024-04-21 19:38

本文主要是介绍使用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源码解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/923875

相关文章

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

java解析jwt中的payload的用法

《java解析jwt中的payload的用法》:本文主要介绍java解析jwt中的payload的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解析jwt中的payload1. 使用 jjwt 库步骤 1:添加依赖步骤 2:解析 JWT2. 使用 N

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr