【问题标题】:OBJC AVSpeechUtterance writeUtterance how?OBJC AVSpeechUtterance writeUtterance 怎么样?
【发布时间】:2020-05-23 09:02:13
【问题描述】:

我正在尝试创建一个 TTS 以在 Objc 中归档。 由于iOS13可以将其写入文件。 但我坚持使用 writeUtterance:toBufferCallback。

有人在objc中有这个函数的例子吗?

[synth speakUtterance:utterance];

【问题讨论】:

  • This answer 可能会回答你的问题。
  • 很快就可以了,但我所有的代码都在 objc 中。

标签: objective-c avspeechsynthesizer avspeechutterance


【解决方案1】:

Referring to the potential answer in Swift,这将是 Objective-C 实现

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"test 123"];
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[utterance setVoice:voice];

__block AVAudioFile *output = nil;

[synthesizer writeUtterance:utterance
           toBufferCallback:^(AVAudioBuffer * _Nonnull buffer) {
    AVAudioPCMBuffer *pcmBuffer = (AVAudioPCMBuffer*)buffer;
    if (!pcmBuffer) {
        NSLog(@"Error");
        return;
    }
    if (pcmBuffer.frameLength != 0) {
        //append buffer to file
        if (output == nil) {
            output = [[AVAudioFile alloc] initForWriting:[NSURL fileURLWithPath:@"test.caf"]
                                                settings:pcmBuffer.format.settings
                                            commonFormat:AVAudioPCMFormatInt16
                                             interleaved:NO error:nil];
        }
        [output writeFromBuffer:pcmBuffer error:nil];
    }
}];

【讨论】:

  • 太棒了!谢谢!完美运行。现在我明白了。
猜你喜欢
  • 2020-06-18
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 2012-10-23
  • 2015-12-17
相关资源
最近更新 更多