OGeek|极客世界-中国程序员成长平台

标题: ios - 播放奇怪问题时的 AVAudioSession 记录 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:24
标题: ios - 播放奇怪问题时的 AVAudioSession 记录

对于我正在制作的应用程序的一部分,我需要录制来自用户的音频。我选择使用 AVAudioRecorder 来执行此操作。

问题是当我开始录制音频时,设备上播放的所有音频都会暂停。然后,我查看了 Apple 文档中的 AVAudioSession 并将录制选项设置为:AVAudioSessionCategoryOptionMixWithOthers

问题在于,每当我开始录制时,设备上播放的任何音频都会从主外部扬声器切换到小型电话(通话)扬声器。

我的目标是录制音频,同时仍允许设备中的所有其他音频通过主扬声器输出。

我怎样才能做到这一点?

这是我目前的代码:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
    [audioSession setActive:YES error:&error];

    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

    if(recorder != nil){
        [recorder prepareToRecord];
        recorder.meteringEnabled = YES;
        [recorder record];
        levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selectorselector(levelTimerCallback userInfo:nil repeats:YES];
    }else{
        NSLog(@"Error: %@",[error description]);
    }//end if

谢谢!



Best Answer-推荐答案


也许您可以尝试使用 AVAudioSessionCategoryOptionDefaultToSpeaker。根据 AVAudioSession引用:

When using this option and no other audio route (such as a headset) is available, session audio will play through the device’s built-in speaker. When not using this option, and no other audio output is available or selected, audio will play through the receiver (a speaker intended to be held to the ear).

关于ios - 播放奇怪问题时的 AVAudioSession 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24276016/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4