【问题标题】:AVAudioSession mixing with AVSpeechUtteranceAVAudioSession 与 AVSpeechUtterance 混合
【发布时间】:2015-07-29 20:50:14
【问题描述】:

我有一个问题,我想让 iPod 在后台播放音乐,在我的应用程序中我想让AVSpeechUtterance 降低 iPod 的音量,播放一串文本,然后我想让 iPod 继续播放。我已经尝试了几件事,但从来没有让 AVAudioSession 和 AVSpeechUtterance 一起工作得很好。我可以让 iPod 停止,暂停一会儿,然后重新开始播放。我可以看到我的字符串被传递给AVSpeechUtterance,但它不会优先于扬声器播放。这是我的代码:

视图已加载:

   - (void)viewDidLoad
   {
            //set the audio session for my app to play from the TTS library
            NSError *error;
            audioSession = [AVAudioSession sharedInstance];
            [audioSession setCategory:AVAudioSessionCategoryAmbient withOptions:1  error:&error];
            [audioSession setActive:YES error:&error];
            if(error){
                NSLog(@"Audio Error %@", error);
            }

            syn = [[AVSpeechSynthesizer alloc] init];
            syn.delegate = self;

            iPodMusicPlayer = [[MPMusicPlayerController alloc] init];

   }

这是我的语音功能:

   -(void)speech:(NSString *)string
   {
             if(audioSession.isOtherAudioPlaying){
                 wasPlaying = YES;
                 NSLog(@"Other audio is playing");
                 [iPodMusicPlayer pause];
             }

             AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:string];
             utterance.rate = 0.3f;
             utterance.volume = 1.0;
             [syn speakUtterance:utterance];

   }

AVSpeechSynthesizer演讲结束后调用的委托方法

   - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
   {
         if(wasPlaying){
            NSLog(@"Start Playing");
            [iPodMusicPlayer play];
            wasPlaying = NO;
         }
   }

我查看了AVAudioSessionhere 上的 Apple 文档,看起来我已经正确实施了一切。有谁知道我哪里出错了?

【问题讨论】:

    标签: ios objective-c avaudiosession avspeechsynthesizer


    【解决方案1】:

    我只是想让大家知道,在进行了一些故障排除后,这就是我能够解决我的问题的方法。我不确定我是否喜欢这个解决方案,并且正在努力改进它。

    查看已加载

        - (void)viewDidLoad
        {
                syn = [[AVSpeechSynthesizer alloc] init];
                syn.delegate = self;
    
                iPodMusicPlayer = [[MPMusicPlayerController alloc] init];
        }
    

    语音功能

        -(void)speak:(NSString *)string
        {
    
                audioSession = [AVAudioSession sharedInstance];
    
                NSError *setCategoryError = nil;
                NSError *activationError = nil;
                BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback
                    withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&setCategoryError];
    
                [audioSession setActive:YES error:&activationError];
    
                if(audioSession.isOtherAudioPlaying){
                    wasPlaying = YES;
                    NSLog(@"Other audio is playing");
                    [iPodMusicPlayer pause];
                }
    
                NSLog(@"Success %hhd", success);
    
                AVSpeechUtterance *utterance = [AVSpeechUtterance           speechUtteranceWithString:string];
                utterance.rate = 0.3f;
                utterance.volume = 1.0;
                [syn speakUtterance:utterance];
         }
    

    语音完成后调用的 AVSpeechSynthesizer 委托方法

         - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
         {
                if(wasPlaying){
                    NSLog(@"Start Playing");
                   [iPodMusicPlayer play];
                   wasPlaying = NO;
                }
         }
    

    【讨论】:

    • 这里的事情是你不处理其他应用程序,例如播客,因为你明确暂停和恢复 PodMusicPlayer。
    • 这不只是在演讲进行时暂停音乐吗?我以为你想让演讲超越音乐?
    • 您还可以使用 AVAudioSessionCategoryOptionDuckOthers 在合成器说话时软化音乐。无需暂停。
    猜你喜欢
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2014-07-07
    相关资源
    最近更新 更多