iOS 百度语音识别 SDK 简单使用

一、接入指南

第一步:找到Baidu-Voice-SDK-iOS-1.6.2和JSONKit第三方库添加到工程中

第二步:在info.plist文件中设置请求数据网络添加NSAppTransportSecurity在他的文件下添加NSAllowsArbitraryLoads后面Boolean改成YES;

第三步:在Build Phases/Compile Sources中找到JSONKit 在后面添加上-fno-objc-arc

第三步:引入编译需要的Framework:/依赖库

GLKit.framework

CoreTelephony.framework

AVFoundation.framework

SystemConfiguration.framework

AudioToolbox.framework

libz.tbd

Security.framework

QuartzCore.framework

CoreText.framework

CoreLocation.framework

CFNetwork.framework

CoreGraphics.framework

二、使用

1.在ViewController.m中引入头文件

#import "BDRecognizerViewController.h"

#import "BDRecognizerViewDelegate.h"

#import "BDVoiceRecognitionClient.h"

#import "BDVRRawDataRecognizer.h"

#import "BDVRFileRecognizer.h"

三、实现

@interface ViewController (){

    BDRecognizerViewController *bdvc;

    NSMutableData *Mdata;

    BDRecognizerViewParamsObject *bdvp;

    UITextView *textV;

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(100, 100, 100, 30);

    button.backgroundColor = [UIColor lightGrayColor];

    button.layer.cornerRadius = 10;

    [button setTitle:@"点击说话" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    BDTheme *me = [BDTheme lightBlueTheme];

    //初始化

    bdvc = [[BDRecognizerViewController alloc] initWithOrigin:CGPointMake(20, 100) withTheme:me];

    //设置代理

    bdvc.delegate = self;

    //全屏

    bdvc.enableFullScreenMode = YES;


    bdvp = [[BDRecognizerViewParamsObject alloc] init];


    bdvp.apiKey = @"S0UrbTi31Yi6hTGjssCk0hzR";

    bdvp.secretKey = @"adb44d9f61aa57d86bcf970572c2f568";

    //初始化文本框

    textV = [[UITextView alloc] initWithFrame:CGRectMake(100, 200, 200, 200)];

    textV.layer.borderWidth = 1;

    [self.view addSubview:textV];

}

- (void)clickButton

{

    Mdata = [[NSMutableData alloc] init];


    [bdvc startWithParams:bdvp];

}

#pragma mark -- 代理方法

/**

* @brief 语音识别结果返回,搜索和输入模式结果返回的结构不相同

*

* @param aBDRecognizerView 弹窗UI

* @param aResults 返回结果,搜索结果为数组,输入结果也为数组,但元素为字典

*/

- (void)onEndWithViews:(BDRecognizerViewController *)aBDRecognizerViewController withResults:(NSArray *)aResults

{

    textV.text = [[[[aResults objectAtIndex:0]objectAtIndex:0]allKeys]objectAtIndex:0];


}

/**

* @brief 录音数据返回

*

* @param recordData 录音数据

* @param sampleRate 采样率

*/

- (void)onRecordDataArrived:(NSData *)recordData sampleRate:(int)sampleRate

{

    //拼接

    [Mdata appendData:recordData];

}

你可能感兴趣的:(iOS 百度语音识别 SDK 简单使用)