2016-08-07 5 views
0

저는 응용 프로그램에서 MPMusicPlayerController 및 AVSpeechSynthesizer 클래스를 처음 사용하고 있습니다. MPMusicPlayerController를 사용하여 음악을 재생하고 5 분마다 (AVSpeechSynthesizer를 사용하여) 통계로 러너를 업데이트하는 실행중인 앱입니다. 그것은 잘 작동하지만 음악과 방송은 같은 볼륨에 있으므로 노래를 듣는 것에 따라 통계를 듣기가 어려울 수 있습니다. 따라서 통계가 방송되는 동안 음악 볼륨을 낮추고 싶습니다. 아래의 코드는 통계가 방송되기 시작했을 때만 음악 볼륨을 줄이기 위해 작동하지만 통계 방송이 끝난 후에는 음악을 다시 시작하지 않습니다. 물론 내가 원하는 일입니다. 나는이 게시물 Setting iOS MPMusicPlayerController volume relative to AVAudioPlayer 에서이 솔루션을 사용하고 있습니다. 내 코드는 다음과 같습니다 :더킹을 구현하려는 MPMusicPlayerController 및 AVSpeechSynthesizer

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

답변

0

사용이 그들이 오디오 재생 재개 다른 응용 프로그램에게 : 다시 시작하려는 오디오가 자신의 응용 프로그램에서 오는 경우

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

}

이 대리자 메서드에서 재생을 다시 시작합니다
관련 문제