首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AVAudioRecorder块AVSpeechSynthesizer

AVAudioRecorder块AVSpeechSynthesizer
EN

Stack Overflow用户
提问于 2014-03-17 19:47:13
回答 1查看 1.2K关注 0票数 2

我用Cordova 3.2表示文本到语音,语音到文本。在iOS 7下,AVSpeechSynthesizer是可用的,工作非常好。下面是插件的关键部分:

代码语言:javascript
运行
复制
        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),语音到文本转换的响应会干扰合成器的输出。

在我试图解决这个问题时,我注意到了一些事情:

  1. 在模拟器中运行,没有问题。事实上,我必须小心等待演讲结束后再录音,否则录音会通过麦克风选择输出。
  2. 在iPad 3w/ iOS 7+上,启动录音将暂停合成器输出,直到录制的文件被回放。在成功录制之后释放对文件的媒体引用。
  3. 录音后,合成器委托接收响应: TTSPlugin确实开始说,TTSPlugin将在语音串的范围内发言。TTSPlugin将在语音串的范围内发言。TTSPlugin将在语音串的范围内发言。TTSPlugin确实取消了讲话
  4. 取消语音合成,清除话语队列。

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

编辑

我解决了问题。罪魁祸首是科多瓦的媒体插件AVAudioSession。我以前没有做过多个音频源,所以这是一个支柱。

我将这些添加到我的TTS插件中,以管理AVAudioSession并根据需要激活它。现在一切都好了。

代码语言:javascript
运行
复制
// 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]);
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-12-03 07:17:29

媒体中的startRecordingAudio方法(CDVSound.m)将AVAudioSession设置为"AVAudioSessionCategoryRecord“。

在我的例子中,我只是在下面添加了一行,它对我有效。

代码语言:javascript
运行
复制
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[self.synthesizer speakUtterance:utt];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22463928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档