我用Cordova 3.2表示文本到语音,语音到文本。在iOS 7下,AVSpeechSynthesizer是可用的,工作非常好。下面是插件的关键部分:
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
self.synthesizer.delegate = self;
NSString * toBeSpoken=[command.arguments objectAtIndex:0];
NSNumber * rate=[command.arguments objectAtIndex:1];
NSString * voice=[command.arguments objectAtIndex:2];
NSNumber * volume=[command.arguments objectAtIndex:3];
NSNumber * pitchMult=[command.arguments objectAtIndex:4];
AVSpeechUtterance *utt =[[AVSpeechUtterance alloc] initWithString:toBeSpoken];
utt.rate= [rate floatValue]/4;// this is to slow down the speech rate
utt.volume=[volume floatValue];
utt.pitchMultiplier=[pitchMult floatValue];
utt.voice=[AVSpeechSynthesisVoice voiceWithLanguage:voice];
[self.synthesizer speakUtterance:utt];问题发生在课文讲完之后。使用Cordova to record (AVAudioRecorder),语音到文本转换的响应会干扰合成器的输出。
在我试图解决这个问题时,我注意到了一些事情:
我的目标是能够与应用程序进行对话。我找不到干扰的地方。帮助?
编辑
我解决了问题。罪魁祸首是科多瓦的媒体插件AVAudioSession。我以前没有做过多个音频源,所以这是一个支柱。
我将这些添加到我的TTS插件中,以管理AVAudioSession并根据需要激活它。现在一切都好了。
// returns whether or not audioSession is available - creates it if necessary
- (BOOL)hasAudioSession
{
BOOL bSession = YES;
if (!self.avSession) {
NSError* error = nil;
self.avSession = [AVAudioSession sharedInstance];
if (error) {
// is not fatal if can't get AVAudioSession , just log the error
NSLog(@"error creating audio session: %@", [[error userInfo] description]);
self.avSession = nil;
bSession = NO;
}
}
return bSession;
}
- (void)setAudioSession
{
if ([self hasAudioSession]) {
NSError* __autoreleasing err = nil;
NSNumber* playAudioWhenScreenIsLocked = 0;
BOOL bPlayAudioWhenScreenIsLocked = YES;
if (playAudioWhenScreenIsLocked != nil) {
bPlayAudioWhenScreenIsLocked = [playAudioWhenScreenIsLocked boolValue];
}
NSString* sessionCategory = bPlayAudioWhenScreenIsLocked ? AVAudioSessionCategoryPlayback : AVAudioSessionCategorySoloAmbient;
[self.avSession setCategory:sessionCategory error:&err];
if (![self.avSession setActive:YES error:&err]) {
// other audio with higher priority that does not allow mixing could cause this to fail
NSLog(@"Unable to play audio: %@", [err localizedFailureReason]);
}
}
}发布于 2014-12-03 07:17:29
媒体中的startRecordingAudio方法(CDVSound.m)将AVAudioSession设置为"AVAudioSessionCategoryRecord“。
在我的例子中,我只是在下面添加了一行,它对我有效。
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[self.synthesizer speakUtterance:utt];https://stackoverflow.com/questions/22463928
复制相似问题