语音生成二维码

第三个
第2个


导入第三方文件


ViewController

#import "QRCodeGenerator.h"
#import "BDRecognizerViewController.h"
#import "BDRecognizerViewDelegate.h"
#import "BDVoiceRecognitionClient.h"
#import "BDVRRawDataRecognizer.h"
#import "BDVRFileRecognizer.h"
#import <AVFoundation/AVFoundation.h>


@interface ViewController () <BDRecognizerViewDelegate>

{

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

}


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

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容