導(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ù)。