iOS內建文字轉語音

來源:互聯網
上載者:User

標籤:can   通話   lin   imu   target   action   int   gray   elf   

  最近做項目時使用到要將一段文本通過按鈕點擊控制轉換成語音朗讀,之前一直不知道系統有這個功能,所以今記錄下來,以便於下次使用。之前自己的項目中曾經使用過訊飛的文字轉語音技術,但是通過實際測試,發現它的免費線上轉語音不是很好,受網路的影響聲音經常斷斷續續的;而它的離線轉語音價格有太貴,最便宜的要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 的標頭檔中去查看它的別的方法的使用與介紹。

iOS內建文字轉語音

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.