2

我将 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 Media 调用来记录 (AVAudioRecorder) 语音到文本转换的响应会破坏合成器的输出。

在我试图弄清楚这一点时,我注意到了一些事情:

  1. 在模拟器中运行,没有问题。事实上,我要小心等待演讲结束后再录音,否则录音会通过麦克风拾取输出。

  2. 在带有 iOS 7+ 的 iPad 3 上,开始录制将暂停合成器输出,直到播放录制的文件。成功录制后释放对该文件的媒体引用。

  3. 录制后,合成器委托接收响应:

    TTSPlugin 确实开始说话 TTSPlugin 将在语音字符串范围内说话。TTSPlugin 将在语音字符串范围内说话。TTSPlugin 将在语音字符串范围内说话。TTSPlugin 确实取消了说话

  4. 取消语音合成会清除话语队列。

我的目标是能够与应用程序进行对话。我找不到干扰在哪里。帮助?

编辑

我解决了这个问题。罪魁祸首是 AVAudioSession,Cordova 中的媒体插件正在管理它。我之前没有做过多个音频源,所以这很糟糕。

我将这些添加到我的 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]);
        }
    }
}
4

1 回答 1

0

Media(CDVSound.m) 中的 startRecordingAudio 方法将 AVAudioSession 设置为“AVAudioSessionCategoryRecord”。

就我而言,我只是添加了以下 1 行,它对我有用。

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[self.synthesizer speakUtterance:utt];
于 2014-12-03T07:17:29.597 回答