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

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详