HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何让我的应用音频在讲话时很好地中断iPhone音频大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的iOS 7应用程序在必要时发出文本.

我想做的是让用户在我的运行时收听他的音乐或播客(或使用音频的任何其他应用).

预期的行为是,当我的应用说话时,其他音频会混音或躲避,然后其他音频会在之后的初始级别恢复音量.

我已经尝试了很多方法来实现这个目标,但没有什么是足够好的,因为我在代码之后列出了我面临的问题.

我目前的实现基于在播放或文本到语音之前创建会话,如下所示:

+ (void)setAudioActive {

    [[self class] setSessionActiveWithmixing:YES];
}

播放/演讲结束后,我将我设置为闲置如下:

+ (void)setAudioIdle {
    [[self class] setSessionActiveWithmixing:NO];
}

核心功能,根据活动参数处理会话设置,如下所示:

+ (void)setSessionActiveWithmixing:(BOOL)active
{
    NSError *error = NULL;
    BOOL     success;

    AVAudioSession *session = [AVAudioSession sharedInstance];

    static NSInteger counter = 0;

    success = [session setActive:NO error:&error];
    if (error) {
        DDLogError(@"startAu@L_950_3@mixAndBACkground: session setActive:NO,%@",error.description);
    }
    else {
        counter--; if (counter<0) counter = 0;
    }

    if (activE) {
        AVAudioSessionCategoryOptions options = AVAudioSessionCategoryOptionAllowBluetooth
            //|AVAudioSessionCategoryOptionDefaultToSpeaker
            |AVAudioSessionCategoryOptionDuckOthers
        ;


        success = [session setCategory://AVAudioSessionCategoryPlayBACk
                   AVAudioSessionCategoryPlayAndRecord
                           withOptions: options
                                 error: &error];
        if (error) {
            // Do some error handling
            DDLogError(@"startAu@L_950_3@mixAndBACkground: setCategory:AVAudioSessionCategoryPlayBACk,error.description);
        }
        else {
            //activate the audio session
            success = [session setActive:YES error:&error];
            if (error) {
                DDLogError(@"startAu@L_950_3@mixAndBACkground: session setActive:YES,error.description);
            }
            else {
                counter++;
            }
        }
    }

    DDLogInfo(@"Audio session counter is: %ld",counter);
}

我目前的问题是:

1)当我的应用程序开始讲话时,我听到声音中有一些小故障,这使它不好;

2)当我将路由连接到蓝牙时,基础音频(比如播客或iPod音乐)变得非常低并且听起来很嘈杂,这使得我的解决方案仅仅无法使用,我的用户将拒绝此级别的低质量.

3)当其他蓝牙连接的设备试图发出声音时(例如汽车或实例中的GPS),我的应用程序没有收到任何中断(或我处理错误),请参阅我的代码如下:

- (void)startAu@L_950_3@mixAndBACkground {

    // initialize our AudioSession -
    // this function has to be called once before calling any other AudioSession functions
    [[NsnotificationCenter defaultCenter] addObserver:self SELEctor:@SELEctor(audioSessionDidChangeInterruptionType:)
                                                 name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];

    // set our default audio session state
    [[self class] setAudioIdle];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    if ([self canBecomeFirstResponder]) {
        [self becomeFirstResponder];
    }

    @synchronized(self) {
        self.okToPlaySound = YES;
    }

    //MPVolumeSetTingSALErtShow();
}
//  want remote control events (via Control Center,headphones,bluetooth,AirPlay,etc.)
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl)
    {
        switch(event.subtypE)
        {
            case UIEventSubtypeRemoteControlPause:
            case UIEventSubtypeRemoteControlStop:
                [[self class] setAudioIdle];
                break;
            case UIEventSubtypeRemoteControlPlay:
                [[self class] setAudioActive];
                break;
            default:
                break;
        }
    }
}

#pragma mark - Audio Support

- (void)audioSessionDidChangeInterruptionType:(Nsnotification *)notification
{
    AVAudioSessionInterruptiontype interruptionType = [[[notification userInfo]
                                                        objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];

    if (AVAudioSessionInterruptionTypeBegan == interruptionTypE)
    {
       DDLogVerbose(@"Session interrupted: --- Begin Interruption ---");
    }
    else if (AVAudioSessionInterruptionTypeEnded == interruptionTypE)
    {
        DDLogVerbose(@"Session interrupted: --- End Interruption ---");
    }
}

解决方法

您的问题很可能是由于您正在设置的类别:AVAudioSessionCategoryPlayAndRecord. PlayAndRecord类别不允许您的应用与其他应用混合/播放音频.你应该在这里再次引用音频会话类别的文档: https://developer.apple.com/library/ios/documentation/avfoundation/reference/AVAudioSession_ClassReference/Reference/Reference.html.看起来AVAudioSessionCategoryAmbient可能更像你想要的.

大佬总结

以上是大佬教程为你收集整理的ios – 如何让我的应用音频在讲话时很好地中断iPhone音频全部内容,希望文章能够帮你解决ios – 如何让我的应用音频在讲话时很好地中断iPhone音频所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。