用于phonegap pauseSpeakingAtBoundary的ios AVSpeechSynthesizer插件不起作用

Posted

技术标签:

【中文标题】用于phonegap pauseSpeakingAtBoundary的ios AVSpeechSynthesizer插件不起作用【英文标题】:ios AVSpeechSynthesizer plugin for phonegap pauseSpeakingAtBoundary not working 【发布时间】:2013-11-25 11:44:51 【问题描述】:

我为 phonegap 制作了一个插件,允许用户使用 AVSpeechSynthesizer 听到一段文本,但我似乎无法让 pauseSpeakingAtBoundary 工作。

出于测试目的,它当前接收要合成的文本字符串或显示“暂停”的字符串,并仅检查 if (![echo isEqual:@'PAUSE']) 以确定是否应尝试暂停话语。讲话开始并在收到“PAUSE”时记录下来,但 合成器继续说话。

我对此完全陌生,所以我不确定我是否犯了错误或pauseSpeakingAtBoudary 有问题。 我的代码如下。谢谢。

重申一下,我无法开始工作的是 pauseSpeakingAtBoundary。根据 phonegaps 文档,语音合成由 javascript 执行程序运行。

//
//  Echo.h
//  Plugin
//
//
//
//

#import <Cordova/CDVPlugin.h>
#import <AVFoundation/AVFoundation.h>


@interface Echo : CDVPlugin <AVSpeechSynthesizerDelegate>


@property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;

- (void) echo:(NSMutableArray*)arguments;



@end


//
//  Echo.m
//  Plugin
//
//
//
//

#import "Echo.h"

@implementation Echo



- (void)echo:(CDVInvokedUrlCommand*)command



    CDVPluginResult* pluginResult = nil;
    NSString* echo = [command.arguments objectAtIndex:0];

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

    synthesizer.delegate = self;

    if (echo != nil && [echo length] > 0 ) 

    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];

    if( ![echo  isEqual:@"PAUSE"]) 

        AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:echo];
        utterance.rate = 0.20;
        utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-gb"];
        [synthesizer speakUtterance:utterance];

     else 


        NSLog(@"Pausing");

        [synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];


    





 else 
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];


[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];



@end

【问题讨论】:

【参考方案1】:

我设法通过将 pauseSpeaking... 和 continueSpeaking 分离为不同的函数并从 javascript exec 执行它们来使其工作。这就是 Echo.m 的外观:

//
//  Echo.m
//  Plugin
//
//
//
//

#import "Echo.h"

@implementation Echo





- (void)echo:(CDVInvokedUrlCommand*)command


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

CDVPluginResult* pluginResult = nil;
NSString* echo = [command.arguments objectAtIndex:0];


if (echo != nil && [echo length] > 0 ) 

    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];


        AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:echo];
        utterance.rate = 0.20;
        utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-gb"];
        [self.synthesizer speakUtterance:utterance];     


 else 
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];


[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];


-(void)speechSynthesizerPause:(AVSpeechSynthesizer *)synthesizer 

[self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
NSLog(@"Pausing");



-(void)speechSynthesizerContinue:(AVSpeechSynthesizer *)synthesizer 

   [self.synthesizer continueSpeaking];
    NSLog(@"Continue");




-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance 
NSLog(@"Playback finished");



@end

【讨论】:

以上是关于用于phonegap pauseSpeakingAtBoundary的ios AVSpeechSynthesizer插件不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Phonegap 仅用于移动开发

Phonegap fileTransfer.Download 不适用于 ios phonegap 3.0

哪个IDE用于在Phonegap中开发应用程序?

InAppPurchases 不适用于 PhoneGap 应用程序

相机插件不适用于 Android 的 PhoneGap Build

Facebook 插件不适用于 phonegap 构建