首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OBJC AVSpeechUtterance writeUtterance如何?

OBJC AVSpeechUtterance writeUtterance如何?
EN

Stack Overflow用户
提问于 2020-05-23 17:02:13
回答 1查看 259关注 0票数 0

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

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

[synth speakUtterance:utterance];

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-23 22:35:51

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

代码语言:javascript
复制
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];
    }
}];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61969696

复制
相关文章

相似问题

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