iOS開(kāi)發(fā)<記錄> 文字轉(zhuǎn)語(yǔ)音iOS10

導(dǎo)入框架:

  import <AVFoundation/AVFoundation.h>

創(chuàng)建

   @interface ViewController ()<AVSpeechSynthesizerDelegate>
  {
      AVSpeechSynthesizer * av;
  }

添加按鈕

/語(yǔ)言播放
UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];

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

[button setTitle:@"講"forState:UIControlStateNormal];

[button setTitle:@"停"forState:UIControlStateSelected];

[button setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];

button.backgroundColor=[UIColor grayColor];

button.showsTouchWhenHighlighted=YES;

[button addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

設(shè)置代理并且實(shí)現(xiàn)

  -(void)start:(UIButton*)sender{
if(sender.selected==NO) {
    if([av isPaused]) {
        //如果暫停則恢復(fù),會(huì)從暫停的地方繼續(xù)
        [av continueSpeaking];
        sender.selected=!sender.selected;
    }else{
        //初始化對(duì)象
        av= [[AVSpeechSynthesizer alloc]init];
        av.delegate=self;//代理
        AVSpeechUtterance*utterance = [[AVSpeechUtterance alloc]initWithString:@"英語(yǔ)文章 How To Cover Your Tracks On The Internet2008-06-25 所屬欄目:Security Website Security - Creating a Bulletproof Site in 5 Easy Steps 2008-06"];//需要轉(zhuǎn)換的文字
        
        utterance.rate=0.5;// 設(shè)置語(yǔ)速,范圍0-1,注意0最慢,1最快;
        AVSpeechSynthesisVoice*voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//設(shè)置發(fā)音,這是中文普通話(huà)
        utterance.voice= voice;
        [av speakUtterance:utterance];//開(kāi)始
        sender.selected=!sender.selected;
                }
}else{
    [av pauseSpeakingAtBoundary:AVSpeechBoundaryWord];//暫停
    sender.selected=!sender.selected;
}
  }
  - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance{
      NSLog(@"---開(kāi)始播放");
  }
  - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance{
      NSLog(@"---完成播放");
  }
  - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance{
      NSLog(@"---播放中止");
  }
  - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance{
      NSLog(@"---恢復(fù)播放");
  }
    - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer     didCancelSpeechUtterance:(AVSpeechUtterance*)utterance{
      NSLog(@"---播放取消");
  }
?著作權(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)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 30,264評(píng)論 8 265
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,653評(píng)論 19 139
  • 今天早上,6:50的鬧鐘聲準(zhǔn)時(shí)響起,而我還在睡夢(mèng)中掙扎。其實(shí)并沒(méi)有做夢(mèng),心里空空如也。 當(dāng)我敏感的神經(jīng)還是一下子就...
    清源泉閱讀 552評(píng)論 0 0
  • 愛(ài)需要氧氣, 我需要你, 但我缺少了想你的勇氣。 當(dāng)我的淚飄落海角天涯時(shí), 而你的縮影卻總能勾起我想你的回憶! 你...
    繹文閱讀 285評(píng)論 0 0
  • 余生未必有你們,但我愿把你們寫(xiě)進(jìn)詩(shī)里,祝福你們…… 陳女多才藝 墨云自變晴 祎碧潔無(wú)暇 …… 白玉酒一杯 不...
    董小東閱讀 716評(píng)論 14 11

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