IOS AVSpeechSynthesis文字轉(zhuǎn)語音

作用:

用文本轉(zhuǎn)化成語音。例如輸入文本“hello world",則系統(tǒng)語音讀出來


前提引入

#import <AVFoundation/AVFoundation.h>

/**
 核心代碼
 */
-(void)func_textToVoice
{
    NSLog(@"開始轉(zhuǎn)化語音");
    
    //要轉(zhuǎn)化的文本
    NSString * text = self.array_text[0];
    
    //語言配置類
    AVSpeechUtterance * utterance = [[AVSpeechUtterance alloc]initWithString:text];
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CH"];
    //TODO...
    
    //語音合成器
    [self.synthesizer speakUtterance:utterance];
}

批量轉(zhuǎn)化語音

/**
 多條文本轉(zhuǎn)換語音
 */
-(void)func_moreTextsToVoice
{
    self.array_text = @[
                        @"除夕更闌人不睡,厭禳鈍滯迎新歲;",
                        @"小兒呼叫走長街,云有癡呆召人買。",
                        @"二物于人誰獨(dú)無?就中吳儂仍有余;",
                        @"巷南巷北賣不得,相逢大笑相揶揄。",
                        @"櫟翁塊坐重簾下,獨(dú)要買添令問價(jià)。",
                        @"兒云翁買不須錢,奉賒癡呆千百年。",
                        ];
    for(int i=0;i<self.array_text.count;i++)
    {
        AVSpeechUtterance * utterance = [[AVSpeechUtterance alloc]initWithString:array_text[i]];
        utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
//        utterance.postUtteranceDelay = 0.1f;
//        utterance.preUtteranceDelay = 0.5f;
        utterance.pitchMultiplier = 0.8f;
        //TODO...

        [self.synthesizer speakUtterance:utterance];
    }

}

設(shè)置語言屬性,如音調(diào)、前后延遲間隔、語速、音量等

/**
 個(gè)性化配置語音
 */
-(void)func_configVoice
{
    //要轉(zhuǎn)化的文本
    NSString * text = self.array_text[0];
    
    //語言配置類
    AVSpeechUtterance * utterance = [[AVSpeechUtterance alloc]initWithString:text];
    //定制語種
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CH"];
    //定制基線音調(diào),范圍0.5~2.0,默認(rèn)1.0
    utterance.pitchMultiplier = 1.0;
    //定制語速,建議用常量來控制范圍 AVSpeechUtteranceMinimumSpeechRate AVSpeechUtteranceMaximumSpeechRate AVSpeechUtteranceDefaultSpeechRate.
    utterance.rate = AVSpeechUtteranceDefaultSpeechRate;
    //定制音量
    utterance.volume = 1.0;
    //該語音播放完后延遲時(shí)間再播放下一個(gè)
    utterance.postUtteranceDelay = 0.5f;
    //等待延遲時(shí)間再進(jìn)行播放
    utterance.preUtteranceDelay = 0.1;
    //TODO...
    
    //語音合成器
    [self.synthesizer speakUtterance:utterance];
}

語音合成事件協(xié)議

#pragma AVSpeechSynthesizerDelegate
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance
{
    NSLog(@"語音合成開始");
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance
{
    NSLog(@"語音合成暫停");
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance
{
    NSLog(@"語音合成取消");
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
{
    NSLog(@"語音合成完成");
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance
{
    NSLog(@"語音合成即將播放第%lu個(gè)字長度為%lu",(unsigned long)characterRange.location,(unsigned long)characterRange.length);
}

暫停

/**
 暫停語音播放
 */
-(void)func_pauseVoice
{
    if(self.synthesizer.speaking)
    {
        [self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];//立即停止
        [self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryWord];//讀完停止
    }
}

繼續(xù)

/**
 繼續(xù)播放
 */
-(void)func_continueSpeaking
{
    if(self.synthesizer.paused)
    {
        [self.synthesizer continueSpeaking];
    }
}

QQ:384302147

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容