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 的头文件中去查看它的别的方法的使用与介绍。

原文地址:https://www.cnblogs.com/zhufengshibei/p/7997586.html