• 科大讯飞语音识别


    先到科大讯飞开放平台注册账号,创建应用并下载SDK,解压SDK压缩包,将framework拖到工程中,如下图

    然后还要导入一下类库:具体如下图

    创建一个工程,我们分成两部分,第一部分就是在FristViewController中,实现语音识别功能,第二部分就是在SecondViewController中,实现文字转换为语音

    首先在AppDelegate.m导入头文件

    1 // 导入头文件
    2 #import <iflyMSC/iflyMSC.h>

    然后在AppDelegate.m中实现登录科大讯飞语音平台

    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】

     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
  • 相关阅读:
    OC与Swift的区别二(常量、变量、运算符)
    OC与Swift的区别一(文件结构)
    OC对象的归档及解档浅析
    OC单例模式的实现
    oc文件基本读写及操作
    IOS之沙盒(Sandbox)机制
    IOS开发之KVC与KVO简述
    SpringMVC控制器配置文件
    spring常用的连接池属性文件配置
    Struts2文件上传方式与上传失败解决方式
  • 原文地址:https://www.cnblogs.com/zhizunbao/p/5558299.html
Copyright © 2020-2023  润新知