2013-11-25 44 views
2

我做了一個手機插件,允許用戶聽到使用AVSpeechSynthesizer一段文字,但我似乎無法讓pauseSpeakingAtBoundary工作。IOS手機AVSpeechSynthesizer插件pauseSpeakingAtBoundary不工作

出於測試目的,它目前接收到一個要合成的文本字符串或一個字符串,說'暫停',只是檢查我確定它是否應該嘗試暫停話語。講話開始並在收到「暫停」時記錄,但合成器繼續講話。

我對此完全陌生,因此我不確定自己是否犯了錯誤或者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 ...來繼續工作,然後繼續討論不同的函數,並從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