I have made a plugin for phonegap that allows users to hear a piece of text using AVSpeechSynthesizer
but I can't seem to get the pauseSpeakingAtBoundary
to work.
For testing purposes it currently receives a string of text to be synthesised or a string that says 'PAUSE' and just checks if (![echo isEqual:@'PAUSE']
) to determine whether it should try to pause the utterance. The speaking starts and it logs when 'PAUSE' has been received but the
synthesizer continues speaking.
I am completely new to this so I am unsure whether I've made a mistake or there is a problem with pauseSpeakingAtBoudary
.
My code is below. Thanks.
Just to reiterate, it's the pauseSpeakingAtBoundary that I can't get to work. The speech synthesis is working from a javascript exec as per phonegaps documentation.
//
// 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
I managed to get it to work by separating the pauseSpeaking... and continueSpeaking into different functions and executing them from the javascript exec. This is how the Echo.m should look:
//
// 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