現在語音服務越來越熱,我們平時使用的很多軟件都帶有語音合成和識別功能,用起來也很方便。說到語音服務,Google和微軟都提供過API接口,不過筆者要介紹的是國內的智能語音技術提供商---科大訊飛。之前看過一個比較Google、微軟和科大訊飛語音識別引擎的博文(http://fqctyj.blog.163.com/blog/static/70843455201361955322797/),有興趣可以去看看。筆者接觸語音服務的時間也不長,對語音服務也不是很了解,但是拆解過科大訊飛的Demo,對語音服務的程序使用還是知道的。這次只整理了語音合成的代碼,關於語音識別和其他的下次再發,廢話完了進入正題。
如何實現語音合成呢?
一、首先到科大訊飛官網注冊賬號(http://open.voicecloud.cn/),並創建應用獲取appid,下載sdk文件
二、代碼實現api調用
1.先用xcode(我這里使用的是xcode 5.1)新建好一個項目,然后在項目添加要用的類庫。其中有一個是訊飛語音的類庫iflyMSC,在下載的sdk文件里有,導入就行了。導入的時候要注意把iflyMSC類庫拷貝到你的工程目錄里,不然后果很嚴重!
2.導完類庫之后,在建好的工程里添加好要用的頭文件。
MainViewController.h
1 #import <UIKit/UIKit.h> 2 #import "iflyMSC/IFlySpeechSynthesizerDelegate.h"
MainViewController.m
1 #import "MainViewController.h" 2 #import <QuartzCore/QuartzCore.h> 3 #import <AVFoundation/AVAudioSession.h> 4 #import <AudioToolbox/AudioSession.h> 5 #import "iflyMSC/IFlySpeechConstant.h" 6 #import "iflyMSC/IFlySpeechUtility.h" 7 #import "iflyMSC/IFlySpeechSynthesizer.h"
3.完成這些准備工作之后,接下來就是堆代碼的工作了。為了方便,筆者只用了兩個控件:一個UITextField、一個UIButton,然后給這兩個控件分別做一個Outlet和Action連接。
MainViewController.h
1 #import <UIKit/UIKit.h> 2 #import "iflyMSC/IFlySpeechSynthesizerDelegate.h" 3 //引入語音合成類 4 @class IFlySpeechSynthesizer; 5 @class IFlyDataUploader; 6 //注意要添加語音合成代理 7 @interface MainViewController : UIViewController<IFlySpeechSynthesizerDelegate> 8 //聲明語音合成的對象 9 @property (nonatomic, strong) IFlySpeechSynthesizer *iFlySpeechSynthesizer; 10 @property (strong, nonatomic) IBOutlet UITextField *content; 11 12 - (IBAction)Start:(id)sender; 13 @end
MainViewController.m
1 #import "MainViewController.h" 2 #import <QuartzCore/QuartzCore.h> 3 #import <AVFoundation/AVAudioSession.h> 4 #import <AudioToolbox/AudioSession.h> 5 #import "iflyMSC/IFlySpeechConstant.h" 6 #import "iflyMSC/IFlySpeechUtility.h" 7 #import "iflyMSC/IFlySpeechSynthesizer.h" 8 9 @interface MainViewController () 10 11 @end 12 13 @implementation MainViewController 14 15 - (void)viewDidLoad 16 { 17 [super viewDidLoad]; 18 //通過appid連接訊飛語音服務器,把@"53b5560a"換成你申請的appid 19 NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",@"53b5560a",@"20000"]; 20 //所有服務啟動前,需要確保執行createUtility 21 [IFlySpeechUtility createUtility:initString]; 22 23 //創建合成對象,為單例模式 24 _iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance]; 25 _iFlySpeechSynthesizer.delegate = self; 26 27 //設置語音合成的參數 28 //合成的語速,取值范圍 0~100 29 [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]]; 30 //合成的音量;取值范圍 0~100 31 [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]]; 32 //發音人,默認為”xiaoyan”;可以設置的參數列表可參考個性化發音人列表 33 [_iFlySpeechSynthesizer setParameter:@"xiaoyan" forKey:[IFlySpeechConstant VOICE_NAME]]; 34 //音頻采樣率,目前支持的采樣率有 16000 和 8000 35 [_iFlySpeechSynthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]]; 36 ////asr_audio_path保存錄音文件路徑,如不再需要,設置value為nil表示取消,默認目錄是documents 37 [_iFlySpeechSynthesizer setParameter:"tts.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]]; 38 39 //隱藏鍵盤,點擊空白處 40 UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)]; 41 tapGr.cancelsTouchesInView = NO; 42 [self.view addGestureRecognizer:tapGr]; 43 } 44 45 -(void)viewTapped:(UITapGestureRecognizer*)tapGr 46 { 47 [self.content resignFirstResponder]; 48 } 49 50 - (void)didReceiveMemoryWarning 51 { 52 [super didReceiveMemoryWarning]; 53 // Dispose of any resources that can be recreated. 54 } 55 56 - (IBAction)Start:(id)sender 57 { 58 //啟動合成會話 59 [_iFlySpeechSynthesizer startSpeaking:self.content.text]; 60 } 61 62 #pragma mark - IFlySpeechSynthesizerDelegate 63 //開始播放 64 - (void) onSpeakBegin 65 { 66 67 } 68 69 //緩沖進度 70 - (void) onBufferProgress:(int) progress message:(NSString *)msg 71 { 72 NSLog(@"bufferProgress:%d,message:%@",progress,msg); 73 } 74 75 //播放進度 76 - (void) onSpeakProgress:(int) progress 77 { 78 NSLog(@"play progress:%d",progress); 79 } 80 81 //暫停播放 82 - (void) onSpeakPaused 83 { 84 85 } 86 87 //恢復播放 88 - (void) onSpeakResumed 89 { 90 91 } 92 93 //結束回調 94 - (void) onCompleted:(IFlySpeechError *) error 95 { 96 97 } 98 @end
4.以上的代理方法其實是可以不寫的,但是官方給出的說明是需要加上的。若是在運行過程中出現錯誤,可以查看開發者文檔的錯誤碼列表,找出相應的錯誤。如果大家對以上的講解還有不懂的地方,請仔細閱讀官方提供的sdk文件,里面有詳細的說明,也可以給我留言。
PS:若本文有什么錯誤的地方,還望大家指正留言,我會盡快修改!