iOS自帶文字轉語音


  最近做項目時使用到要將一段文本通過按鈕點擊控制轉換成語音朗讀,之前一直不知道系統有這個功能,所以今記錄下來,以便於下次使用。之前自己的項目中曾經使用過訊飛的文字轉語音技術,但是通過實際測試,發現它的免費在線轉語音不是很好,受網絡的影響聲音經常斷斷續續的;而它的離線轉語音價格有太貴,最便宜的要8000RMB/2000裝機量,令許多開發者望而止步。那么既然支付不了訊飛那昂貴的費用,iOS自帶的文字轉語音也是我們不錯的選擇,只是他的發音效果不夠理想,不過還可以接受。在iOS7之前,想要實現語音播報文字內容,可能需要第三方資源庫來實現。現在在iOS7之后,系統為我們提供了語音播報文字的功能,我們不僅可以播報英語內容,也可以播報漢語文字。

首先我們創建一個按鈕來操控:

UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];

startButton.frame = CGRectMake(100,100,100,50);

[startButton setTitle:@"開始朗讀"forState:UIControlStateNormal];

[startButton setTitleColor:[UIColorblueColor] forState:UIControlStateNormal];

startButton.backgroundColor = [UIColor grayColor];

startButton.showsTouchWhenHighlighted = YES;

[startButton addTarget:selfaction:@selector(speakText:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:startButton];

然后正式開始:

第一步:需要在 target - Build Phases - Link Binary With Libraries 導入 AVFoundation.framework 框架。

第二部:在需要用到的類中導入頭文件 #import<AVFoundation/AVSpeechSynthesis.h> ,如果需要使用到代理的話,則需要遵守代理 AVSpeechSynthesizerDelegate。

第三部:創建一個全局的變量,以便於我們的控制 :AVSpeechSynthesizer *synth; 如果你不需要暫停、繼續播放等操作的話可以創建一個局部的。

第四部:在按鈕的點擊事件中

- (IBAction)speakText:(id)sender {

    if( [[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0)  // 判斷系統是否大於或等於 7.0

    {

        if ([synth isPaused]) {

            //如果暫停則恢復,會從暫停的地方繼續

            [synth continueSpeaking];

        } else {

    //需要轉換的文字

    NSString *str = @"一個是閬苑仙葩,一個是美玉無瑕。若說沒奇緣,今生偏又遇着他;若說有奇緣,如何心事終虛化?一個枉自嗟呀,一個空勞牽掛。一個是水中月,一個是鏡中花。想眼中能有多少淚珠兒,怎禁得秋流到冬盡,春流到夏!";

            AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:str];

            utterance.rate = 0.5; // 設置語速,范圍0-1,注意0最慢,1最快;(AVSpeechUtteranceMinimumSpeechRate最慢,AVSpeechUtteranceMaximumSpeechRate最快)

            synth = [[AVSpeechSynthesizer alloc] init];

            synth.delegate = self;//設置代理

            //獲取當前系統語音

            NSString *preferredLang = @"";

            //設置發音,這是中文普通話

            preferredLang = @"zh-CN";

            AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:[NSString stringWithFormat:@"%@",preferredLang]];

            utterance.voice = voice;

            [synth speakUtterance:utterance]; // 開始朗讀

        }

    }

}

再創建一個按鈕用來控制 暫停播放或停止播放

UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeCustom];

stopButton.frame = CGRectMake(100,100,100,50);

[stopButton setTitle:@"停止朗讀"forState:UIControlStateNormal];

[stopButton setTitleColor:[UIColorblueColor] forState:UIControlStateNormal];

stopButton.backgroundColor = [UIColor grayColor];

stopButton.showsTouchWhenHighlighted = YES;

[stopButton addTarget:selfaction:@selector(stopSpeakText:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:stopButton];

// 停止/暫停播放

- (IBAction)stopSpeakText:(id)sender {

    [synth stopSpeakingAtBoundary:AVSpeechBoundaryWord];//停止播放,調用這個方法,再開始時會從頭開始重新朗讀

  //[synth pauseSpeakingAtBoundary:AVSpeechBoundaryWord];//暫停播放,調用這個方法,再開始時會從暫停的地方繼續播放

}

#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(@"---取消播放");

}

基本的使用就這些了,當然想了解更多的話,可以進入到 AVSpeechSynthesizer 的頭文件中去查看它的別的方法的使用與介紹。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM