使用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

相关文章

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected