iOS desarrollo-AVSpeechSynthesizer función de síntesis de voz (texto a voz) de AVFAudio

iOS desarrollo-AVSpeechSynthesizer función de síntesis de voz (texto a voz) de AVFAudio

En el desarrollo anterior, me encontré con la necesidad de texto a voz. Afortunadamente, el sistema proporciona una API para implementar esta función, es decir, AVSpeechSynthesizer en AVFAudio.
Puede usar AVSpeechSynthesizer proporcionado por AVFAudio para implementar la función de texto a voz (síntesis de voz).Esta clase se usa para reproducir uno o más contenidos de idioma, y ​​estos contenidos de voz son instancias de la clase AVSpeechUtterance.

1. Sintetizador AVSpeech

AVSpeechSynthesizer es un objeto que genera voz sintetizada a partir de expresiones de texto y puede monitorear o controlar el habla en curso.

Al reproducir texto, puede crear una instancia de AVSpeechUtterance que contenga el texto y pasarlo a una instancia de AVSpeechSynthesizer para reproducir a través de speakUtterance:.

AVSpeechSynthesizer mantendrá una cola. Si no hay ninguna tarea ejecutándose actualmente, comenzará a sintetizar inmediatamente. Si actualmente está ejecutando una tarea de síntesis, la agregará a la cola y las sintetizará en el orden recibido.

El método AVSpeechSynthesizerDelegate se puede utilizar para determinar si reproducir y si la reproducción está completa.

Dos, código de síntesis de voz AVSpeechSynthesizer

el código se muestra a continuación

#import "INAVSpeechPresenter.h"
#import <AVFAudio/AVSpeechSynthesis.h>

@implementation INAVSpeechPresenter

- (void)didStart:(UIButton *)sender {
    
    
    if(sender.selected == NO) {
    
    
        if([self.avSpeech isPaused]) {
    
    
            //如果暂停则恢复,会从暂停的地方继续
            [self.avSpeech continueSpeaking];
            sender.selected = !sender.selected;
        }else{
    
    
            // 初始化对象
            AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:@"收款9865.05元"];//需要转换的文字
            utterance.rate = 0.2;// 设置语速,范围0-1,注意0最慢,1最快;AVSpeechUtteranceMinimumSpeechRate最慢,AVSpeechUtteranceMaximumSpeechRate最快
            AVSpeechSynthesisVoice*voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//设置发音,这是中文普通话
            utterance.voice = voice;
            [self.avSpeech speakUtterance:utterance];//开始
            sender.selected = !sender.selected;
        }
    } else {
    
    
        //[av stopSpeakingAtBoundary:AVSpeechBoundaryWord];//感觉效果一样,对应代理>>>取消
        [self.avSpeech pauseSpeakingAtBoundary:AVSpeechBoundaryWord];//暂停
        sender.selected = !sender.selected;
    }
}

#pragma mark - AVSpeechSynthesizerDelegate
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance{
    
    
    NSLog(@"---开始播放");
}

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance{
    
    
    NSLog(@"---完成播放");
}

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance{
    
    
    NSLog(@"---播放中止");
}

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance{
    
    
    NSLog(@"---恢复播放");
}

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance*)utterance{
    
    
    NSLog(@"---播放取消");
}

#pragma mark - SETTER/GETTER
- (AVSpeechSynthesizer *)avSpeech {
    
    
    if (!_avSpeech) {
    
    
        _avSpeech = [[AVSpeechSynthesizer alloc] init];
        _avSpeech.delegate = self;
    }
    return _avSpeech;
}

@end

3. Resumen

iOS desarrollo-AVSpeechSynthesizer función de síntesis de voz (texto a voz) de AVFAudio. AVSpeechSynthesizer realiza la función de texto a voz (síntesis de voz).Esta clase se utiliza para reproducir uno o más contenidos de idioma, y ​​estos contenidos de voz son instancias de la clase de AVSpeechUtterance.

Registros de aprendizaje, sigue mejorando cada día.

Supongo que te gusta

Origin blog.csdn.net/gloryFlow/article/details/131991707
Recomendado
Clasificación