百度语音

#import "ViewController.h"

#import "QRCodeGenerator.h"

#import "BDRecognizerViewController.h"

#import "BDRecognizerViewDelegate.h"

#import "BDVoiceRecognitionClient.h"

#import "BDVRRawDataRecognizer.h"

#import "BDVRFileRecognizer.h"

#import

@interface ViewController ()

{

    BDRecognizerViewController *bdvc;

    NSMutableData *Mdata;


    BDRecognizerViewParamsObject *bdvp;

    UITextField *textV;



    AVSpeechSynthesizer *_synthesizer;

    // 实例化说话的语言,说中文、英文

    AVSpeechSynthesisVoice *_voice;


    //创建图片对象


    UIImageView *imagerView;


}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];


    _voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh_CN"];

    // 要朗诵,需要一个语音合成器

    _synthesizer = [[AVSpeechSynthesizer alloc] init];


    UIButton *bu = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    bu.frame = CGRectMake(100, 50, 100, 30);

    bu.backgroundColor = [UIColor lightGrayColor];

    bu.layer.cornerRadius = 10;

    [bu setTitle:@"点击播放" forState:UIControlStateNormal];

    [bu addTarget:self action:@selector(dian) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:bu];




    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 = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 200, 60)];

    textV.layer.borderWidth = 1;

    [self.view addSubview:textV];


    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];


    btn.frame=CGRectMake(100, 140, 100, 30);

    btn.backgroundColor = [UIColor lightGrayColor];

    btn.layer.cornerRadius = 10;

    //二维码

    [btn setTitle:@"二维码生成" forState:UIControlStateNormal];


    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];


    [self.view addSubview:btn];

    imagerView = [[UIImageView  alloc]initWithFrame:CGRectMake(100, 320, 200, 200)];


    imagerView.backgroundColor = [UIColor whiteColor];


    [self.view addSubview:imagerView];


}

-(void)click{


    //关键是这一行代码(生成二维码)


    UIImage *image = [QRCodeGenerator qrImageForString:textV.text    imageSize:imagerView.bounds.size.width];

    imagerView.image= image;


}

- (void)clickButton

{

    Mdata = [[NSMutableData alloc] init];


    [bdvc startWithParams:bdvp];

}

#pragma mark -- 代理方法

/**

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

 *

 * @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];

}

-(void)dian

{

      AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:textV.text];

    utterance.voice = _voice;

    utterance.rate = 0.3;

    [_synthesizer speakUtterance:utterance];

}

@end

你可能感兴趣的:(百度语音)