標籤:class code tar ext c http
轉至:http://www.sufeinet.com/thread-2488-1-1.html
如何使用訊飛的語音SDK加入到你的iPhone應用中吧!
1.首先請先到訊飛官方網站的開發人員專區(http://open.voicecloud.cn/developer.php)。如果你還沒有註冊,那麼需要先註冊一個開發人員帳號的。點擊網站頂部的“免費註冊”即可,使用你的自己的手機號碼註冊一個帳號。
2.僅僅註冊了一個開發人員帳號還可以使用SDK,因為還需要申請Appid。登陸之後到自己的個人中心建立一個應用填寫好相關資訊,然後等待審核通過,因為只有審核通過之後,你才可以下載訊飛語音的SDK。如下:
<ignore_js_op>
3.如果你的應用已經審核通過,點擊SDK下載,並且選擇對應平台的SDK,比如現在選擇iphone平台SDK下載。
4.下載好了之後,你會看到一個壓縮包,解壓出來有四個檔案,
<ignore_js_op>
其中sample就是訊飛提供的DEMO了,開啟項目,你會看到這樣的目錄結構:
<ignore_js_op>
直接運行,並沒有錯誤,只有幾個警告,運行介面如下:
<ignore_js_op>
< ignore_js_op>
5.那麼怎麼在自己的項目中使用呢?其實還是非常簡單的。訊飛也有提供相關的開發文檔,還是比較詳細。在開發人員專區和下載專區都有文檔可供下載。
6.建立一個iPhone 項目吧。建立好了之後,需要先匯入需要的lib,如下:
<ignore_js_op>
其中需要說明的是,iflyMSC.framework這個lib就在你下載的SDK解壓檔案下的lib檔案夾下。在添加lib的介面選擇Add Others...,並且選擇你下載的lib。點擊Open,就能正確添加。
<ignore_js_op>
7.一些函數和配置的說明:
在你需要使用SDK的標頭檔中匯入檔案:
- #import "iFlyMSC/IFlyRecognizeControl.h"
- #import "iFlyMSC/IFlysynthesizerControl.h"
複製代碼
建立一個識別控制項或者合成控制項:
- IFlyRecognizeControl *_iFlyRecognizeController; //識別控制項
- IFlySynthesizerControl *_iFlySynthesizerControl; //合成控制項
複製代碼
在實現檔案初始化控制項:
- //初始化語音識別控制項
- _iFlyRecognizeController = [[IFlyRecognizeControl alloc] initWithOrigin:CGPointMake(20, 70) initParam:initPara];
- [self.view addSubview:_iFlyRecognizeController];
-
- //Configure
- [_iFlyRecognizeController setEngine:@"sms" engineParam:nil grammarID:nil];
- [_iFlyRecognizeController setSampleRate:16000];
- [_iFlyRecognizeController setDelegate:self];
- [_iFlyRecognizeController setShowLog:NO];
-
- //註冊unActive事件
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignActive) name:UIApplicationWillResignActiveNotification object:nil];
-
- //初始化語音合成控制項
- _iFlySynthesizerControl = [[IFlySynthesizerControl alloc] initWithOrigin:CGPointMake(20, 70) initParam:initPara];
-
- //Configure
- [_iFlySynthesizerControl setDelegate:self];
- [_iFlySynthesizerControl setVoiceName:@"vixm"]; //發音人(中英文粵語)
- [self.view addSubview:_iFlySynthesizerControl];
-
- //show UI
- [_iFlySynthesizerControl setShowUI:YES];
-
- //show log
- [_iFlySynthesizerControl setShowLog:NO];
-
- //註冊unActive事件
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignactiveOfSynthesizer) name:UIApplicationWillResignActiveNotification object:nil];
複製代碼
一些回呼函數和方法:
- //識別結束回調函數-會話過程結束調用此函數
- - (void)onRecognizeEnd:(IFlyRecognizeControl *)iFlyRecognizeControl theError:(int)error
- {
- [_recognizeButton setEnabled:YES];
- [_synthesizerButton setEnabled:YES];
- NSLog(@"識別結束");
- NSLog(@"上傳流量:%d,下載流量:%d",[iFlyRecognizeControl getUpflow:FALSE],[iFlyRecognizeControl getDownflow:FALSE]);
- }
- //識別結果回調函數
- - (void)onResult:(IFlyRecognizeControl *)iFlyRecognizeControl theResult:(NSArray *)resultArray
- {
- NSString *strResult = [[resultArray objectAtIndex:0] objectForKey:@"NAME"];
- NSLog(@"識別的結果是:%@",strResult);
- }
- //開始語音識別
- - (void)onButtonRecognize
- {
- if ([_iFlyRecognizeController start]) {
- [_recognizeButton setEnabled:NO];
- [_synthesizerButton setEnabled:NO];
- }
- }
- //不支援後台模式則在unActive事件時執行cancel
- - (void)resignActive
- {
- [_iFlyRecognizeController cancel];
- }
- //合成回調函數,執行cancel函數整個會話結束調用此函數
- - (void)onSynthesizerEnd:(IFlySynthesizerControl *)iFlySynthesizerControl theError:(int)error
- {
- [_synthesizerButton setEnabled:YES];
- [_recognizeButton setEnabled:YES];
- NSLog(@"結束");
- NSLog(@"上傳流量:%d,下載流量:%d",[iFlySynthesizerControl getUpflow:FALSE],[iFlySynthesizerControl getDownflow:FALSE]);
- }
- //獲取播放器緩衝進度
- - (void)onSynthesizerBufferProgress:(float)bufferProgress
- {
- NSLog(@"當前緩衝進度:%f",bufferProgress);
- }
- //獲取播放器播放進度
- - (void)onSynthesizerPlayProgress:(float)playProgress
- {
- NSLog(@"當前播放進度:%f",playProgress);
- }
- //開始語音合成
- - (void)onButtonSynthesizer
- {
- [_iFlySynthesizerControl setText:@"哈哈,這僅僅是一個測試合成功能的例子而已,你不用太緊張會發生什麼特別的事情。謝謝" params:nil];
- if ([_iFlySynthesizerControl start]) {
- [_recognizeButton setEnabled:NO];
- [_synthesizerButton setEnabled:NO];
- } else {
- NSLog(@"I‘m sorry,start error. ");
- }
- }
- //不支援後台模式則unActive事件時執行cancel
- - (void)resignactiveOfSynthesizer
- {
- NSLog(@"resignActive");
- [_iFlySynthesizerControl cancel];
- }
複製代碼
其中下列方法可以在自己需要開始使用語音功能的地方調用,比如:
- _recognizeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- _synthesizerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [_recognizeButton setFrame:CGRectMake(70, 100, 180, 60)];
- [_synthesizerButton setFrame:CGRectMake(70, 200, 180, 60)];
- [_recognizeButton setTitle:@"開始語音識別" forState:UIControlStateNormal];
- [_synthesizerButton setTitle:@"開始語音合成" forState:UIControlStateNormal];
-
- [_recognizeButton addTarget:self action:@selector(onButtonRecognize) forControlEvents:UIControlEventTouchDown];
- [_synthesizerButton addTarget:self action:@selector(onButtonSynthesizer) forControlEvents:UIControlEventTouchDown];
-
- [self.view addSubview:_recognizeButton];
- [self.view addSubview:_synthesizerButton];
- //開始語音識別
- - (void)onButtonRecognize
- {
- if ([_iFlyRecognizeController start]) {
- [_recognizeButton setEnabled:NO];
- [_synthesizerButton setEnabled:NO];
- }
- }
- //開始語音合成
- - (void)onButtonSynthesizer
- {
- [_iFlySynthesizerControl setText:@"這僅僅是一個測試合成功能的例子,你不用太緊張會發生什麼" params:nil];
- if ([_iFlySynthesizerControl start]) {
- [_recognizeButton setEnabled:NO];
- [_synthesizerButton setEnabled:NO];
- } else {
- NSLog(@"I‘m sorry,start error. ");
- }
- }
複製代碼
8.到這裡已經可以使用語音辨識和合成功能了。如下:
<ignore_js_op>
< ignore_js_op>
< ignore_js_op>