科大訊飛語音識別

先到科大訊飛開放平臺註冊帳號,建立應用並下載SDK,解壓SDK壓縮包,將framework拖到工程中,以下圖前端

而後還要導入一下類庫:具體以下圖後端

建立一個工程,咱們分紅兩部分,第一部分就是在FristViewController中,實現語音識別功能,第二部分就是在SecondViewController中,實現文字轉換爲語音緩存

首先在AppDelegate.m導入頭文件數據結構

1 // 導入頭文件
2 #import <iflyMSC/iflyMSC.h>

而後在AppDelegate.m中實現登陸科大訊飛語音平臺app

1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2     // Override point for customization after application launch.
3     
4     // 第二步:登陸科大訊飛語音平臺 【換成本身的訊飛應用id就好了】
5     NSString *appID = [NSString stringWithFormat:@"appid=%@",@"5750da61"];
6     [IFlySpeechUtility createUtility:appID];
7     
8     return YES;
9 }

到了第一部分的實現了,直接上代碼【FirstViewController.m框架

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

實現第二部分【SecondViewController.m】ide

 1 #import "SecondViewController.h"
 2 
 3 //文字識別的回調方法接口
 4 #import <iflyMSC/IFlySpeechSynthesizerDelegate.h>
 5 //文字識別對象
 6 #import <iflyMSC/IFlySpeechSynthesizer.h>
 7 //科大訊飛語音框架定義的常量
 8 #import <iflyMSC/IFlySpeechConstant.h>
 9 
10 @interface SecondViewController () <IFlySpeechSynthesizerDelegate>  // 遵循相關代理
11 
12 /**  textView  */
13 @property (weak, nonatomic) IBOutlet UITextView *textView;
14 
15 /**  文字識別對象  */
16 @property (strong, nonatomic) IFlySpeechSynthesizer *synthesizer;
17 
18 @end
19 
20 @implementation SecondViewController
21 
22 - (void)viewDidLoad {
23     [super viewDidLoad];
24     
25     
26     //建立文字識別對象
27     self.synthesizer = [IFlySpeechSynthesizer sharedInstance];
28     
29     //指定文字識別對象的代理對象
30     self.synthesizer.delegate = self;
31     
32     //設置文字識別對象的關鍵屬性
33     [self.synthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]];
34     [self.synthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]];
35     [self.synthesizer setParameter:@"XIAOYAN" forKey:[IFlySpeechConstant VOICE_NAME]];
36     [self.synthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
37     [self.synthesizer setParameter:@"temp.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];
38     [self.synthesizer setParameter:@"custom" forKey:[IFlySpeechConstant PARAMS]];
39 }
40 
41 
42 #pragma mark - 識別相關的內容
43 - (IBAction)beginRecognise:(id)sender {
44     
45     [self.synthesizer startSpeaking:_textView.text];
46 }
47 
48 
49 #pragma mark - 代理方法
50 - (void)onCompleted:(IFlySpeechError *)error {
51     
52     NSLog(@"");
53 }
54 
55 @end
相關文章
相關標籤/搜索