暂停AVSpeechSynthesizer的按钮

时间:2014-03-17 18:18:24

标签: ios mkmapview avspeechsynthesizer

目前我有一个带注释的地图,当用户点击注释时,会播放音频。我想添加播放/暂停按钮,但它不起作用,我不确定为什么。

AVSpeechSynthesizer

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)anView
{
//Get a reference to the annotation this view is for...
id<MKAnnotation> annSelected = anView.annotation;

//Before casting, make sure this annotation is our custom type
//(and not some other type like MKUserLocation)...
if ([annSelected isKindOfClass:[MapViewAnnotation class]])
{
    MapViewAnnotation *mva = (MapViewAnnotation *)annSelected;

    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];

    AVSpeechUtterance *utterance =
    [AVSpeechUtterance speechUtteranceWithString:mva.desc];
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-gb"];

    [utterance setRate:0.35];
    [synthesizer speakUtterance:utterance];
}

按钮

- (IBAction)pauseButtonPressed:(UIButton *)sender 
{
[_synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];

}

现在点击它时没有任何反应。

3 个答案:

答案 0 :(得分:1)

use this one for pause

- (IBAction)pausePlayButton:(id)sender
    {
        if([synthesize isSpeaking]) {
            [synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
            AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
            [synthesize speakUtterance:utterance];
            [synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
        }

    }

答案 1 :(得分:0)

我认为你没有初始化_synthesizer。尝试执行self.synthesizer = [[AVSpeechSynthesizer alloc] init];而不是将合成器分配给局部变量。

我注意到AVSpeechSynthesizer在7.0 beta期间很难关闭,但我发现很难相信这样一个令人震惊的bug会持续这么长时间。

注意:每次点击一个注释时,你应该不应该重新创建AVSpeechSynthesizer

NB2:暂停后,我认为您必须致电continueSpeaking重启。

答案 2 :(得分:0)

为了将来的帮助,当我点击暂停并恢复注释时,它不会让我播放另一个注释,因为技术上第一个仍在运行。我添加了一个带[_synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];的停止按钮进行排序。