【问题标题】:MPMusicPlayerController and AVSpeechSynthesizer trying to implement duckingMPMusicPlayerController 和 AVSpeechSynthesizer 试图实现闪避
【发布时间】:2017-03-26 05:03:35
【问题描述】:

我第一次在应用程序中使用 MPMusicPlayerController 和 AVSpeechSynthesizer 类。这是一个正在运行的应用程序,它播放音乐(使用 MPMusicPlayerController)并每 5 分钟更新一次跑步者的统计数据(使用 AVSpeechSynthesizer)。它工作正常,但音乐和广播的音量相同,因此根据播放的歌曲可能很难听到统计数据,所以现在我希望在广播统计数据时音乐音量降低。下面的代码仅在统计数据开始广播时降低音乐音量,但在统计数据广播结束后它不会恢复音乐,这当然是我想要它做的。我正在使用这篇文章Setting iOS MPMusicPlayerController volume relative to AVAudioPlayer 中的这个解决方案。 我的代码如下:

- (void)setAudioSessionWithDucking:(BOOL)isDucking
    {
    AudioSessionSetActive(NO);

    UInt32 overrideCategoryDefaultToSpeaker = 1;
    AudioSessionSetProperty     (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof     (overrideCategoryDefaultToSpeaker), &overrideCategoryDefaultToSpeaker);

    UInt32 overrideCategoryMixWithOthers = 1;
    AudioSessionSetProperty     (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof     (overrideCategoryMixWithOthers), &overrideCategoryMixWithOthers);

    UInt32 value = isDucking;
    AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck,     sizeof(value), &value);

    AudioSessionSetActive(YES);
}

- (void)updateLabels
{

if(fmod(mins,5) == 0){
 [self setAudioSessionWithDucking:YES];

    AVSpeechUtterance *utterance = [AVSpeechUtterance
                                    speechUtteranceWithString:newText];
    AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];

    utterance.rate = 0.45;
    utterance.pitchMultiplier = 0.95;
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"];
    utterance.volume = 1.0;

    [synth speakUtterance:utterance];

 [self setAudioSessionWithDucking:NO];
         }
}

【问题讨论】:

    标签: ios mpmusicplayercontroller avspeechsynthesizer


    【解决方案1】:

    使用它来告诉其他应用他们可以恢复音频播放:

    -(void)notifyIOSthatOtherAppsCanResumeAudioPlayback{
        NSError *err;
        [indigoAudioSession setActive: NO
                      withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
                            error: &err];
        if(err!=nil){
            NSLog(@"audioIsFreeNotificationForIOS ERROR: %@",err.description);
        }
    }
    

    如果您要恢复的音频来自您自己的应用,您可以通过此委托方法告诉它恢复播放:

    - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
        playSomeMusic();
    

    }

    【讨论】:

      猜你喜欢
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2015-12-27
      相关资源
      最近更新 更多