訊飛語音的使用(iOS)

一、介紹

訊飛語音做的相當(dāng)不錯(cuò),容錯(cuò)率達(dá)到90%多,如果需要做語音方面的功能,它絕對(duì)是一個(gè)不錯(cuò)的選擇。訊飛語音的功能很多:語音聽寫、語音識(shí)別、語音合成等,但我們最常用的還是語音聽寫。訊飛語音中包含界面的語音聽寫和不帶界面的語音聽寫,下面我來演示一下。

二、準(zhǔn)備工作

(1) 去訊飛語音開發(fā)平臺(tái)注冊(cè)賬號(hào)并登陸,然后在控制臺(tái)創(chuàng)建應(yīng)用,獲取對(duì)應(yīng)的app id,這個(gè)以后使用它注冊(cè)激活訊飛語音。


(2) 下載訊飛語音SDK,將其拖入到項(xiàng)目中,然后添加需要所有的依賴庫,另外還有新添加庫Contacts.framework,編譯運(yùn)行即可。


(3) 引入頭文件,以及遵從代理,激活訊飛語音,并實(shí)現(xiàn)功能。

三、BitCode 設(shè)置

在 Xcode 7 默認(rèn)開啟了 Bitcode,Bitcode 需要工程依賴的類庫同時(shí)支持。而語記 SDK 暫時(shí)還不支持Bitcode,所以可以先臨時(shí)關(guān)閉。后續(xù)支持 Bitcode 請(qǐng)關(guān)注訊飛開放平臺(tái)版本更新,QQ 群中也會(huì)??醒。在 Targets - Build Settings 中搜索 Bitcode 即可,找 到相應(yīng)選項(xiàng),設(shè)置為 NO。

四、使用

第一種不帶界面的語音聽寫

DDHomeSearchViewController.m

#import <UIKit/UIKit.h>
#import "iflyMSC/iflyMSC.h"
#import "iflyMSC/IFlySpeechRecognizerDelegate.h"
#import "iflyMSC/IFlySpeechRecognizer.h"

@interface DDHomeSearchViewController : UIViewController<IFlySpeechRecognizerDelegate>
@property (nonatomic, strong) IFlySpeechRecognizer *iFlySpeechRecognizer;   //不帶界面的識(shí)別對(duì)象
@end

//初始化Appid
-(void)initAppId
{
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"xxxxxxx"];
    [IFlySpeechUtility createUtility:initString];
}

//初始化識(shí)別器
-(void)initRecognizer
{
    //單例模式,無UI的實(shí)例
    if (_iFlySpeechRecognizer == nil) {
    _iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance];
    }

    // 設(shè)置參數(shù)
    if (_iFlySpeechRecognizer != nil) {
    //擴(kuò)展參數(shù)
    [_iFlySpeechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
    //設(shè)置聽寫模式
    [_iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
    //設(shè)置最長錄音時(shí)間
    [_iFlySpeechRecognizer setParameter:@"30000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
    //設(shè)置后端點(diǎn)
    [_iFlySpeechRecognizer setParameter:@"1800" forKey:[IFlySpeechConstant VAD_EOS]];
    //設(shè)置前端點(diǎn)
    [_iFlySpeechRecognizer setParameter:@"1800" forKey:[IFlySpeechConstant VAD_BOS]];
    //網(wǎng)絡(luò)等待時(shí)間
    [_iFlySpeechRecognizer setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];
    //設(shè)置采樣率,推薦使用16K
    [_iFlySpeechRecognizer setParameter:@"16000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
    //設(shè)置語言
    [_iFlySpeechRecognizer setParameter:@"zh_cn" forKey:[IFlySpeechConstant LANGUAGE]];
    //設(shè)置方言
    [_iFlySpeechRecognizer setParameter:@"mandarin" forKey:[IFlySpeechConstant ACCENT]];
    //設(shè)置是否返回標(biāo)點(diǎn)符號(hào)
    [_iFlySpeechRecognizer setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]];
    //設(shè)置數(shù)據(jù)返回格式
    [_iFlySpeechRecognizer setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];

    }

    // 設(shè)置代理
    _iFlySpeechRecognizer.delegate = self;
}


//實(shí)現(xiàn)代理方法
// 出現(xiàn)錯(cuò)誤
- (void) onError:(IFlySpeechError *) error
{
    NSLog(@"出現(xiàn)錯(cuò)誤 :%@",error);
}

// 識(shí)別結(jié)果 
- (void) onResults:(NSArray *) results isLast:(BOOL)isLast
{
    NSMutableString *result = [[NSMutableString alloc] init];
    NSDictionary *dic = [results objectAtIndex:0];
    for (NSString *key in dic) {
        [result appendFormat:@"\n\n------ %@ (置信度:%@) ------ \n\n",key,[dic objectForKey:key]];
    }

    // 忽略結(jié)束。號(hào)
    if ([result hasPrefix:@"\n\n------ 。"]) {
        return;
    }

    NSLog(@"%@",result);
}


//點(diǎn)擊開始和結(jié)束
- (IBAction)understand:(id)sender
{   
    // 開始識(shí)別
    [_iFlySpeechRecognizer startListening];
}

- (IBAction)finish:(id)sender
{   
    // 停止識(shí)別
    [_iFlySpeechRecognizer stopListening];
}


//初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAppId];
    [self initRecognizer];
}
第二種帶界面的語音聽寫:

DDHomeSearchViewController.m

//
//  DDHomeSearchViewController.m//
//  Created by 夏遠(yuǎn)全 on 16/12/1.
//  Copyright ? 2016年 . All rights reserved.
//

#import "DDHomeSearchController.h"

@interface DDHomeSearchViewController ()<UISearchBarDelegate,IFlyRecognizerViewDelegate>
{
    IFlyRecognizerView      *_iflyRecognizerView;
}
@property (nonatomic, strong) UISearchBar * searchBar;
@end

@implementation DDHomeSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    //初始化導(dǎo)航欄
    [self _createNavigationBar];
    
    //初始化語音識(shí)別
    [self initIFly];
    [self initAppid];
}


//創(chuàng)建導(dǎo)航欄
- (void)_createNavigationBar {
    
    //取消
    self.navigationItem.leftBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) ImageName:@"back" Title:@"取消" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(back)];
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    
    //搜索欄
    _searchBar.placeholder = @"請(qǐng)輸入要搜索的商家、品類";
    _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    _searchBar.autoresizesSubviews = YES;
    _searchBar.delegate = self;
    self.navigationItem.titleView = _searchBar;
    
    //搜索
    self.navigationItem.rightBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) Title:@"語音" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(voiceStart)];
}

//返回
-(void)back{
    [self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
}


//初始化Appid
-(void)initAppid{
    
    //注冊(cè)訊飛語音
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"xxxxxxx"];
    [IFlySpeechUtility createUtility:initString];
}


//語音識(shí)別初始化
-(void)initIFly{
    
    //單例模式,無UI的實(shí)例
    if (_iflyRecognizerView == nil) {
        _iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
        _iflyRecognizerView.delegate = self;
    }
    
    // 設(shè)置參數(shù)
    if (_iflyRecognizerView != nil) {
        //擴(kuò)展參數(shù)
        [_iflyRecognizerView setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
        //設(shè)置聽寫模式
        [_iflyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
        //設(shè)置最長錄音時(shí)間
        [_iflyRecognizerView setParameter:@"30000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
        //設(shè)置后端點(diǎn)
        [_iflyRecognizerView setParameter:@"1800" forKey:[IFlySpeechConstant VAD_EOS]];
        //設(shè)置前端點(diǎn)
        [_iflyRecognizerView setParameter:@"1800" forKey:[IFlySpeechConstant VAD_BOS]];
        //網(wǎng)絡(luò)等待時(shí)間
        [_iflyRecognizerView setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];
        //設(shè)置采樣率,推薦使用16K
        [_iflyRecognizerView setParameter:@"16000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
        //設(shè)置語言
        [_iflyRecognizerView setParameter:@"zh_cn" forKey:[IFlySpeechConstant LANGUAGE]];
        //設(shè)置方言
        [_iflyRecognizerView setParameter:@"mandarin" forKey:[IFlySpeechConstant ACCENT]];
        //設(shè)置是否返回標(biāo)點(diǎn)符號(hào)
        [_iflyRecognizerView setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]];
        //設(shè)置數(shù)據(jù)返回格式
        [_iflyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
    }
}

//語音搜索啟動(dòng)
-(void)voiceStart{
    //按鈕變換
    self.navigationItem.rightBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) Title:@"結(jié)束" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(voiceEnd)];
    
    //啟動(dòng)識(shí)別服務(wù)
    [_iflyRecognizerView start];
}

//結(jié)束錄音
-(void)voiceEnd{
    
    //按鈕變換
    self.navigationItem.rightBarButtonItems = [UIBarButtonItem itemWithFrame:CGRectMake(0, 0, 60, 40) Title:@"語音" TitleColor:[UIColor whiteColor] size:14 target:self action:@selector(voiceStart)];
    
    //結(jié)束識(shí)別服務(wù)
    [_iflyRecognizerView cancel];
}

/*識(shí)別結(jié)果返回代理
 @param resultArray 識(shí)別結(jié)果
 @ param isLast 表示是否最后一次結(jié)果
 */
- (void)onResult: (NSArray *)resultArray isLast:(BOOL) isLast
{
    [resultArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DDLog(@"%@",obj);
    }];
}
/*識(shí)別會(huì)話錯(cuò)誤返回代理
 @ param  error 錯(cuò)誤碼
 */
- (void)onError: (IFlySpeechError *) error
{
    DDLog(@"語音識(shí)別出錯(cuò):%@",error);
}

#pragma mark - UISearchBarDelegate
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [searchBar becomeFirstResponder];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    DDLog(@"%@",searchText);
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    [searchBar setText:nil];
}
@end
效果截圖(說一句“附近的商店”)

參考:
https://www.cnblogs.com/XYQ-208910/p/6124418.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容