OBJC AVSpeechUtterance написать сообщение как?

Я пытаюсь создать TTS для файла в Objc. Поскольку iOS13 может записать его в файл. Но я застрял с writeUtterance: toBufferCallback.

У кого-нибудь есть пример с этой функцией в objc?

[synth speakUtterance:utterance];


person Mickall    schedule 23.05.2020    source источник
comment
Этот ответ, вероятно, отвечает на ваш вопрос.   -  person Martijn    schedule 23.05.2020
comment
В быстром порядке, но весь мой код находится в objc.   -  person Mickall    schedule 23.05.2020


Ответы (1)


Ссылаясь на потенциальный ответ в 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];
    }
}];
person Martijn    schedule 23.05.2020
comment
Большой! Спасибо! Прекрасно работает. Теперь я понимаю. - person Mickall; 23.05.2020