2017-06-03 110 views
0

我打电话的iOS文本到语音在一个相当标准的方法:完成iOS文本到语音转换工作时铃声静音

static AVSpeechSynthesizer* synthesizer = NULL; 
//... 
+(void)readText:(NSString*)text 
{ 
    if(synthesizer == NULL) 
    synthesizer = [[AVSpeechSynthesizer alloc] init]; 
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:text]; 
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"fr-FR"]; 
    [synthesizer speakUtterance:utterance]; 
} 

它的工作,但有一个问题:当铃声是在设备静​​音,文本到语音也是静音的。即使铃声静音,我如何使其工作?

+6

我猜想这正是铃声是为制作。因此,没有像您这样的开发人员可以绕过静音状态并播放声音,而用户期望手机保持安静。 – LinusGeffarth

+0

以前我认为铃声仅用于静音电话和信息。现在很明显,它比这更复杂:[http://artoftheiphone.com/2012/02/10/basics-what-does-the-iphone-ringer-switch-mute-and-not-mute/]。尽管如此,许多应用程序确实会在铃声关闭的情况下发出声音(例如,浏览器正在播放YouTube),我想绕过它,因为这似乎是我预期的行为。 – Antonio

+0

@LinusGeffarth nope,即使设备静音,也可以激活音频。 –

回答

1

只是取消静音装置

static AVSpeechSynthesizer* synthesizer = NULL; 
//... 
+(void)readText:(NSString*)text 
{ 
    if(synthesizer == NULL) 
    synthesizer = [[AVSpeechSynthesizer alloc] init]; 
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:text]; 
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"fr-FR"]; 

    //Add the following code to active audio 
    NSError *setCategoryErr = nil; 
    NSError *activationErr = nil; 
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr]; 
    //magic is here 
    [[AVAudioSession sharedInstance] setActive:YES error:&activationErr]; 

    //speech your text 
    [synthesizer speakUtterance:utterance]; 
} 
+0

但苹果会批准吗?如[Apple的应用程序审阅指南](https://developer.apple.com/app-store/review/guidelines/#software-requirements)第2.5.9节中所述,应用程序可能不会更改标准开关的功能。更改当前的振铃状态可能会被视为违反本指南。 – LinusGeffarth

+0

那么,脸谱,Instagram和其他许多应用程序使用它 –

+0

1)你怎么知道? 2)Facebook从来不是一个好的参考,也不应该是推特。这些应用程序确实有特殊的权限他们甚至本地集成到操作系统中! – LinusGeffarth

1

的iOS音频会话类别是这个正确答案:https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategoriesandModes/AudioSessionCategoriesandModes.html

的默认值是AVAudioSessionCategorySoloAmbient,但如果音频功能是至关重要的应用程序(我们的情况下),应设置为AVAudioSessionCategoryPlayback

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];