科大訊飛語音識別


1.科大訊飛開放平台 

2.科大訊飛iOS - API開放平台

        那下面我們來看一下科大訊飛的開發步驟吧

第一步:申請賬號ID

登陸到訊飛開放平台上,在用戶菜單欄里創建應用,這里的登陸也可以采用第三方的方式,在創建應用的界面填寫相關的信息即可,然后就會有一個SDK的下載鏈接,如果沒有直接去SDK選項下下載即可。

第二步:導入訊飛SDK框架

下載下來SDK解壓后有三個文件夾:doc文件夾:不用多說肯定是開發文檔;重要的是接下來的那兩個文件夾:一個是lib文件夾:存放科大訊飛SDK類庫,這就是我們要導入的SDK;Sample:iOS的科大訊飛demo演示工程。

下面我們來創建一個工程,將lib文件夾下的“iflyMSC.framework”拷貝到工程目錄,然后在工程中添加依賴庫,如下圖所示:

第三步:開始進行語音識別了

語音識別分兩種,分別用在不同場合,一個是界面提示的語音識別,一個是無界面提示的語音識別,這里以有界面提示的語音識別為例先進性講解。

3.1導入頭文件

 1 #import "iflyMSC/IFlySpeechUtility.h" 

3.2登陸科大訊飛語音平台

在使用訊飛的語音解析之前,需要進行用戶身份驗證,即登陸訊飛服務器,即訊飛服務器需要根據你當前用戶的APPID才能同意你登陸。代碼如下:

 

1     //第二步:登陸科大訊飛語音平台
2     NSString *appID = [NSString stringWithFormat:@"appid=%@",@"570f0a8b"];
3     [IFlySpeechUtility createUtility:appID];

 

3.3創建有界面提示語音識別對象

創建一個訊飛語音識別對象,可以對他進行一系列的調用
 1 #import "FirstViewController.h"
 2 
 3 //第一步:引入庫文件
 4 //科大訊飛語音識別功能回調方法的接口文件
 5 #import <iflyMSC/IFlyRecognizerViewDelegate.h>
 6 //科大訊飛語音識別功能的聲音識別視圖
 7 #import <iflyMSC/IFlyRecognizerView.h>
 8 //科大訊飛語音識別功能中定義的常量
 9 #import <iflyMSC/IFlySpeechConstant.h>
10 
11 ///遵循代理協議
12 @interface FirstViewController ()<IFlyRecognizerViewDelegate>
13 
14 ///語音識別對象
15 @property (nonatomic,strong)IFlyRecognizerView *iflyRecognizerView;
16 
17 ///接收相關結果的字符串
18 @property (nonatomic,strong)NSMutableString *result;
19 
20 ///展示識別內容的textView
21 @property (weak, nonatomic) IBOutlet UITextView *showContentTextView;
22 
23 
24 
25 @end
26 
27 @implementation FirstViewController
28 
29 - (void)viewDidLoad {
30     [super viewDidLoad];
31     // Do any additional setup after loading the view.
32     
33     
34     //創建聲音識別視圖對象,初始化聲音識別控件
35     self.iflyRecognizerView= [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
36     //delegate需要設置,確保delegate回調可以正常返回
37     self.iflyRecognizerView.delegate = self;
38 
39 }
40 
41 #pragma mark - 開始識別
42 - (IBAction)beginRecognise:(id)sender {
43     
44     [self startListenning];
45     
46 }
47 
48 - (void)startListenning
49 {
50     //設置語音識別結果應用為普通文本領域
51     [self.iflyRecognizerView setParameter: @"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
52     //設置前端點檢測時間為6000ms
53     [self.iflyRecognizerView setParameter: @"6000" forKey:[IFlySpeechConstant VAD_BOS]];
54     //設置后端點檢測時間為700ms
55     [self.iflyRecognizerView setParameter: @"700" forKey:[IFlySpeechConstant VAD_EOS]];
56     //設置采樣率為8000
57     [self.iflyRecognizerView setParameter: @"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
58     //設置為返回結果中包含標點符號
59     [self.iflyRecognizerView setParameter: @"1" forKey:[IFlySpeechConstant ASR_PTT]];
60     //設置語音識別完成后數據的返回數據結構類型xml
61     [self.iflyRecognizerView setParameter: @"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
62     //設置在Documents文件夾下緩存的文件名為temp.asr
63     [self.iflyRecognizerView setParameter: @"temp.asr" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
64     //設置自定義的參數
65     [self.iflyRecognizerView setParameter: @"custom" forKey:[IFlySpeechConstant PARAMS]];
66     
67     [self.iflyRecognizerView start];
68     
69 }
70 
71 #pragma mark - 代理方法
72 //成功
73 - (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast{
74     
75     
76     self.result = [[NSMutableString alloc] init];
77     NSDictionary *dic = [resultArray objectAtIndex:0];
78     
79     for (NSString *key in dic)
80     {
81         [self.result appendFormat:@"%@",key];
82     }
83     NSLog(@"%@---------",_result);
84     
85     //自定義控件顯示內容
86     self.showContentTextView.text = [NSString stringWithFormat:@"%@%@",self.showContentTextView.text,self.result];
87 }
88 
89 //失敗
90 - (void)onError:(IFlySpeechError *)error{
91    
92     NSLog(@"%@",error);
93 }

4.1文字識別的回調方法接口

 1 #import "SecondViewController.h"
 2 
 3 //第一步:引入頭文件
 4 //文字識別的回調方法接口
 5 #import <iflyMSC/IFlySpeechSynthesizerDelegate.h>
 6 //文字識別對象
 7 #import <iflyMSC/IFlySpeechSynthesizer.h>
 8 //科大訊飛語音框架定義的常量
 9 #import <iflyMSC/IFlySpeechConstant.h>
10 
11 @interface SecondViewController ()<IFlySpeechSynthesizerDelegate>
12 
13 //文字識別對象
14 @property (strong, nonatomic) IFlySpeechSynthesizer *synthesizer;
15 
16 
17 
18 ///輸入內容的文本框
19 @property (weak, nonatomic) IBOutlet UITextView *inputContentTextView;
20 
21 @end
22 
23 @implementation SecondViewController
24 
25 - (void)viewDidLoad {
26     [super viewDidLoad];
27     // Do any additional setup after loading the view.
28     
29     //創建文字識別對象
30     self.synthesizer = [IFlySpeechSynthesizer sharedInstance];
31     
32     //指定文字識別對象的代理對象
33     self.synthesizer.delegate = self;
34     
35     //設置文字識別對象的關鍵屬性
36     [self.synthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]];
37     [self.synthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]];
38     [self.synthesizer setParameter:@"XIAOYAN" forKey:[IFlySpeechConstant VOICE_NAME]];
39     [self.synthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
40     [self.synthesizer setParameter:@"temp.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];
41     [self.synthesizer setParameter:@"custom" forKey:[IFlySpeechConstant PARAMS]];
42     
43 }
44 
45 #pragma mark - 識別相關的內容
46 - (IBAction)beginRecognise:(id)sender {
47    
48     [self.synthesizer startSpeaking:@"最后一節課了,大家以后要加油"];
49     
50 }
51 
52 #pragma mark - 代理方法
53 - (void)onCompleted:(IFlySpeechError *)error{
54     
55     NSLog(@"%@",error);
56 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM