我为phonegap制作了一个插件,允许用户使用AVSpeechSynthesizer
听到一段文本,但我似乎无法使用pauseSpeakingAtBoundary
。
出于测试目的,它目前接收一个要合成的文本字符串或一个写着"暂停"的字符串,并只检查i f (![echo isEqual:@'PAUSE']
)以确定是否应该尝试暂停说话。当收到"暂停"时,通话开始并记录,但合成器继续讲话。
我对此完全陌生,所以我不确定我是犯了错误还是pauseSpeakingAtBoudary
有问题。我的代码在下面。谢谢
只是重申一下,我不能上班是因为边界上的穷人。根据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
我设法通过分离pauseSpeaking。。。并继续转换为不同的函数,并从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