【问题标题】:AVSpeechSynthesizer delegate method didStartSpeechUtterance not being calledAVSpeechSynthesizer 委托方法 didStartSpeechUtterance 未被调用
【发布时间】:2015-04-02 16:01:42
【问题描述】:

我在使用 AVSpeechSynthesizer 和话语时遇到问题 (ios 7.1.1)

AVSpeechSynthesizer 在处理程序类中初始化,委托为 self:

self.synthesizer = [[AVSpeechSynthesizer alloc] init];

self.synthesizer.delegate = self;

我在委托中实现了以下方法:

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"Finish");
}

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{
    NSLog(@"Start");

}

我的演讲是通过这种方法调用的:

- (void)speak:(NSString *)spokenWord{

    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:spokenWord];   

    [self.synthesizer speakUtterance:utterance]; 

}

然后我重复调用该方法,例如

[AVhandler speak:_word];
[AVhandler speak:_word];
[AVhandler speak:_word];

我希望在日志中看到: 开始 结束 开始 结束 开始 完成等

但我看到的是: 开始 结束 结束 结束 完成

为什么没有调用 didStartSpeechUtterance?

谢谢。

【问题讨论】:

    标签: ios7 delegates avspeechsynthesizer


    【解决方案1】:

    回答有点晚了,抱歉。为了解决这个问题,还要输入:

    synthesizer.delegate = self;
    

    在-(void)中如下

     - (void)speak:(NSString *)spokenWord{
    
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:spokenWord];   
    
    [self.synthesizer speakUtterance:utterance]; 
    synthesizer.delegate = self;
    
    }
    

    在didFinish中如下:

    -(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"Finish");
    synthesizer.delegate = self;
    }
    

    【讨论】:

      【解决方案2】:

      另外不要忘记在你的 .h 文件中添加对 AVSpeechSynthesizerDelegate 协议的引用,例如:

      @interface ViewController : UIViewController <AVSpeechSynthesizerDelegate>
      

      然后你可以把你的 synthesizer.delegate = self;仅在您的 ViewDidLoad 方法中。

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-02
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        • 1970-01-01
        相关资源
        最近更新 更多