AVAudioSession与AVSpeechUtterance混音

时间:2013-12-05 15:01:50

标签: ios objective-c avaudiosession avspeechsynthesizer

我有一个问题,我想让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;
         }
   }

我在AVAudioSession here上查看了Apple文档,看起来我已正确实施了所有内容。有谁知道我哪里出错了?

1 个答案:

答案 0 :(得分:3)

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

查看已加载

    - (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;
            }
     }