前言:
- 接上一章庐椒,這次說語音合成
-簡(jiǎn)單來說就是你輸入一段文字箱熬,系統(tǒng)分析后能用語音讀出來。
—
打開上次創(chuàng)建好的程序褐缠。
1.先在ViewController.h
中添加
#import <UIKit/UIKit.h>
//語音合成
#import "iflyMSC/IFlySpeechSynthesizerDelegate.h”
引入語音合成類
@class IFlySpeechSynthesizer;
@class IFlyDataUploader;
注意要添加語音合成代理
<IFlySpeechSynthesizerDelegate>
2.然后來到ViewController.m
中
導(dǎo)入頭文件
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVAudioSession.h>
#import <AudioToolbox/AudioSession.h>
#import "iflyMSC/IFlySpeechConstant.h"
#import "iflyMSC/IFlySpeechUtility.h"
#import "iflyMSC/IFlySpeechSynthesizer.h”
的確很多……但是為了不報(bào)錯(cuò),加吧
3.完成這些準(zhǔn)備工作之后风瘦,接下來就是堆代碼的工作了队魏。為了方便,筆者只用了兩個(gè)控件:一個(gè)UITextField弛秋、一個(gè)UIButton器躏,然后給這兩個(gè)控件分別做一個(gè)Outlet和Action連接。
聲明接受要閱讀的文本的textField
property (weak, nonatomic) IBOutlet UITextField *content;
聲明語音合成的對(duì)象
@property (nonatomic, strong) IFlySpeechSynthesizer *iFlySpeechSynthesizer;
因?yàn)檎Z言合成要寫的比較多蟹略,我們就寫在一個(gè)方法里,然后在viewDidLoad
里調(diào)用
- (void)viewDidLoad {
[super viewDidLoad];
[self voiceRead];
}
4.在voiceRead
中
通過appid連接訊飛語音服務(wù)器遏佣,把@"58096c82"換成你申請(qǐng)的appid挖炬,如果不記得appid是什么請(qǐng)看第一章
NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",@"58096c82",@"20000”];
所有服務(wù)啟動(dòng)前,需要確保執(zhí)行createUtility
[IFlySpeechUtility createUtility:initString];
創(chuàng)建合成對(duì)象状婶,為單例模式
_iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];
_iFlySpeechSynthesizer.delegate = self;
設(shè)置語音合成的參數(shù),合成的語速,取值范圍0~100
[_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]];
合成的音量;取值范圍0~100
[_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]];
發(fā)音人,默認(rèn)為”xiaoyan”;可以設(shè)置的參數(shù)列表可參考個(gè)性化發(fā)音人列表
[_iFlySpeechSynthesizer setParameter:@"xiaoyan" forKey:[IFlySpeechConstant VOICE_NAME]];
音頻采樣率,目前支持的采樣率有16000 和 8000
[_iFlySpeechSynthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
asr_audio_path保存錄音文件路徑意敛,如不再需要馅巷,設(shè)置value為nil表示取消,默認(rèn)目錄是documents
[_iFlySpeechSynthesizer setParameter:@"tts.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];
隱藏鍵盤草姻,點(diǎn)擊空白處
UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
tapGr.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGr];
點(diǎn)擊空白處隱藏鍵盤方法
-(void)viewTapped:(UITapGestureRecognizer*)tapGr
{
[self.content resignFirstResponder];
}
```
####5.IFlySpeechSynthesizerDelegate代理方法
開始播放
```
-(void)onSpeakBegin
{
}
```
緩沖進(jìn)度
```
-(void)onBufferProgress:(int)progress message:(NSString *)msg
{
NSLog(@"bufferProgress:%d,message:%@",progress,msg);
}
```
播放進(jìn)度
```
-(void)onSpeakProgress:(int)
progress
{
NSLog(@"playprogress:%d",progress);
}
```
暫停播放
```
-(void)onSpeakPaused
{
}
```
恢復(fù)播放
```
-(void)onSpeakResumed
{
}
```
結(jié)束回調(diào)
```
-(void)onCompleted:(IFlySpeechError *) error
{
}
```
####6.開始語言合成按鈕
```
- (IBAction)Start:(id)sender {
//啟動(dòng)合成會(huì)話
[_iFlySpeechSynthesizer startSpeaking:self.content.text];
}
```
#####以上的代理方法其實(shí)是可以不寫的钓猬,但是官方給出的說明是需要加上的。若是在運(yùn)行過程中出現(xiàn)錯(cuò)誤撩独,可以查看開發(fā)者文檔的錯(cuò)誤碼列表敞曹,找出相應(yīng)的錯(cuò)誤。
然后就……
#完成了综膀!
下集預(yù)告:
【OC功能】科大訊飛(三)語音識(shí)別
---
###參考資料:
>[訊飛開放平臺(tái) iOS開發(fā)文檔](http://www.xfyun.cn/doccenter/iOS)
[ IOS開發(fā)之語音合成(科大訊飛)詳解](http://blog.csdn.net/gf771115/article/details/51544087)