現(xiàn)在語音服務(wù)越來越熱,我們平時使用的很多軟件都帶有語音合成和識別功能,用起來也很方便。說到語音服務(wù),Google和微軟都提供過API接口,不過筆者要介紹的是國內(nèi)的智能語音技術(shù)提供商---科大訊飛。之前看過一個比較Google、微軟和科大訊飛語音識別引擎的博文(http://fqctyj.blog.163.com/blog/static/70843455201361955322797/),有興趣可以去看看。筆者接觸語音服務(wù)的時間也不長,對語音服務(wù)也不是很了解,但是拆解過科大訊飛的Demo,對語音服務(wù)的程序使用還是知道的。這次只整理了語音合成的代碼,關(guān)于語音識別和其他的下次再發(fā),廢話完了進入正題。
如何實現(xiàn)語音合成呢?
一、首先到科大訊飛官網(wǎng)注冊賬號(http://open.voicecloud.cn/),并創(chuàng)建應(yīng)用獲取appid,下載sdk文件
二、代碼實現(xiàn)api調(diào)用
1.先用xcode(我這里使用的是xcode 5.1)新建好一個項目,然后在項目添加要用的類庫。其中有一個是訊飛語音的類庫iflyMSC,在下載的sdk文件里有,導(dǎo)入就行了。導(dǎo)入的時候要注意把iflyMSC類庫拷貝到你的工程目錄里,不然后果很嚴重!
2.導(dǎo)完類庫之后,在建好的工程里添加好要用的頭文件。
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.完成這些準(zhǔn)備工作之后,接下來就是堆代碼的工作了。為了方便,筆者只用了兩個控件:一個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 @end12 13 @implementation MainViewController14 15 - (void)viewDidLoad16 {17 [super viewDidLoad];18 //通過appid連接訊飛語音服務(wù)器,把@"53b5560a"換成你申請的appid19 NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",@"53b5560a",@"20000"];20 //所有服務(wù)啟動前,需要確保執(zhí)行createUtility21 [IFlySpeechUtility createUtility:initString];22 23 //創(chuàng)建合成對象,為單例模式24 _iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];25 _iFlySpeechSynthesizer.delegate = self;26 27 //設(shè)置語音合成的參數(shù)28 //合成的語速,取值范圍 0~10029 [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]];30 //合成的音量;取值范圍 0~10031 [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]];32 //發(fā)音人,默認為”xiaoyan”;可以設(shè)置的參數(shù)列表可參考個性化發(fā)音人列表33 [_iFlySpeechSynthesizer setParameter:@"xiaoyan" forKey:[IFlySpeechConstant VOICE_NAME]];34 //音頻采樣率,目前支持的采樣率有 16000 和 800035 [_iFlySpeechSynthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];36 ////asr_audio_path保存錄音文件路徑,如不再需要,設(shè)置value為nil表示取消,默認目錄是documents37 [_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*)tapGr46 {47 [self.content resignFirstResponder];48 }49 50 - (void)didReceiveMemoryWarning51 {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 - IFlySpeechSynthesizerDelegate63 //開始播放64 - (void) onSpeakBegin65 {66 67 }68 69 //緩沖進度70 - (void) onBufferProgress:(int) progress message:(NSString *)msg71 {72 NSLog(@"bufferProgress:%d,message:%@",progress,msg);73 }74 75 //播放進度76 - (void) onSpeakProgress:(int) progress77 {78 NSLog(@"play progress:%d",progress);79 }80 81 //暫停播放82 - (void) onSpeakPaused83 {84 85 }86 87 //恢復(fù)播放88 - (void) onSpeakResumed89 {90 91 }92 93 //結(jié)束回調(diào)94 - (void) onCompleted:(IFlySpeechError *) error95 {96 97 }98 @end
4.以上的代理方法其實是可以不寫的,但是官方給出的說明是需要加上的。若是在運行過程中出現(xiàn)錯誤,可以查看開發(fā)者文檔的錯誤碼列表,找出相應(yīng)的錯誤。如果大家對以上的講解還有不懂的地方,請仔細閱讀官方提供的sdk文件,里面有詳細的說明,也可以給我留言。
PS:若本文有什么錯誤的地方,還望大家指正留言,我會盡快修改!
|
新聞熱點
疑難解答