gpt4 book ai didi

ios AVSpeechSynthesizer 插件 phonegap pauseSpeakingAtBoundary 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:24 34 4
gpt4 key购买 nike

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

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

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

重申一下,我无法开始工作的原因是 pauseSpeakingAtBoundary。根据 phonegaps 文档,语音合成是通过 javascript exec 进行的。

//
// 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... 和 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

关于ios AVSpeechSynthesizer 插件 phonegap pauseSpeakingAtBoundary 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20192238/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com