AVSpeechUtterance以静音模式发声

Pra*_*lan 10 iphone objective-c ios onutterancecompleted avspeechsynthesizer

我正在使用AVSpeechUtterance来讲述给定的文本.我正在使用下面的代码并在"iPhone Ringer模式"中正常工作,但当我将iPhone更改为"静音模式"时,话语声音变得无声.当它是"静音模式"时,我无法听到发声的声音.我该怎么做才能听到"静音模式"中的话语声音.

AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"Hello World"];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[utterance setRate:AVSpeechUtteranceMinimumSpeechRate];
[utterance setVolume:1];
[self.speechSynthesizer speakUtterance:utterance];
Run Code Online (Sandbox Code Playgroud)

uma*_*nta 14

在代码之前添加以下代码.您也可以在appDelegate中编写相同的代码.

NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
Run Code Online (Sandbox Code Playgroud)


Dan*_*nny 7

Swift 3.0的答案

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch let error as NSError {
    print("Error: Could not set audio category: \(error), \(error.userInfo)")
}

do {
    try AVAudioSession.sharedInstance().setActive(true)
}
catch let error as NSError {
    print("Error: Could not setActive to true: \(error), \(error.userInfo)")
}
Run Code Online (Sandbox Code Playgroud)