美文网首页
1、AVSpeechSynthesizer文本到语音

1、AVSpeechSynthesizer文本到语音

作者: 綪天de汰陽 | 来源:发表于2019-05-05 22:55 被阅读0次

    使用方法

     _synthesizer = [[AVSpeechSynthesizer alloc] init];
     _voices = [@[[AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"],[AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"]] mutableCopy];
     _speechStrings = [@[@"Hello AV Foundation. How are you?",
                            @"I'm well! Thanks for asking.",
                            @"Are you excited about the book?",
                            @"Very! I have always felt so misunderstood",
                            @"What's your favorite feature?",
                            @"Oh, they're all my babies, I couldn't possibly choose.",
                            @"It was great to speak with you",
                            @"The pleasure was all mine! Have fun!"] mutableCopy];
    
    - (void)begainConversation{
        for (NSUInteger i = 0; i < self.speechStrings.count; i++) {
            AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:self.speechStrings[i]];
            utterance.voice = self.voices[i % 2];
            utterance.rate = 0.4f;
            utterance.pitchMultiplier = 1.0f;
            utterance.postUtteranceDelay = 0.1f;
            [self.synthesizer speakUtterance:utterance];
        }
    }
    

    AVSpeechUtterance属性介绍

    + (instancetype)speechUtteranceWithString:(NSString *)string;
    创建AVSpeechUtterance实例,传递字符串到speechUtteranceWithString初始化器

    rate 语音播放速率,数值介于AVSpeechUtteranceMinimumSpeechRateAVSpeechUtteranceMaximumSpeechRate之间,这两个字分别是0.0、1.0,默认是AVSpeechUtteranceDefaultSpeechRate值为0.5

    pitchMultiplier 设置声音音调,值一般介于0.5(低音调) 和 2.0(高音调)之间,默认为1.0            //  [0.5 - 2] Default = 1

    volume 音量,         //  [0-1] Default = 1

    postUtteranceDelay 播放下一段语音有短暂的停留,默认为0.0,可以类似的设置preUtteranceDelay的值

    voice AVSpeechSynthesisVoice类型,设置语音

    支持的常用语言列表

    • zh-HK
    • zh-CN
    • ko-KR
    • en-GB
    • en-ZA
    • en-AU
    • en-US
    • en-IE

    相关文章

      网友评论

          本文标题:1、AVSpeechSynthesizer文本到语音

          本文链接:https://www.haomeiwen.com/subject/mtikoqtx.html