【发布时间】: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