接入科大訊飛語(yǔ)音聽寫,增加語(yǔ)音動(dòng)畫,類似京東語(yǔ)音搜索功能

前言:小白第一次接入科大訊飛語(yǔ)音聽寫,接入還是比較簡(jiǎn)單的,先看效果圖無(wú)UI界面

Demo地址拿去

2017-10-25 16_22_20.gif

效果圖有兩部分,一是接入科大訊飛語(yǔ)音聽寫功能,可以實(shí)現(xiàn)將語(yǔ)音轉(zhuǎn)換成文字。 二是看到的語(yǔ)音音量動(dòng)畫效果,為了更加形象。
接入科大訊飛第一步需要自己去科大訊飛開發(fā)者中心申請(qǐng)應(yīng)用,只有應(yīng)用申請(qǐng)成功才能獲取到AppID才可以正常接入語(yǔ)音聽寫,接入部分大家可以去科大訊飛開發(fā)者中心去看開發(fā)文檔,這里主要講一下運(yùn)用到工程中
1.獲取APPID, 添加服務(wù),下載對(duì)應(yīng)的SDK,拿到SDK里邊的iflyMSC.framework,放入自己創(chuàng)建好的工程中
ED4F6CD0-00EC-442A-A820-9AAA6FE580EA.png
2.添加對(duì)應(yīng)的framework依賴
C7639B3F-A060-4997-9253-B3C21031FA0E.png
正常到這里,接入訊飛語(yǔ)音基本完成,下邊是代碼部分
AppDelegate里邊按著開發(fā)者文檔走
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    ViewController * vc = [[ViewController alloc]init];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    [self createOtherFrameWork];
    
    
    return YES;
}
// 初始化訊飛
- (void)createOtherFrameWork{
    //設(shè)置sdk的log等級(jí),log保存在下面設(shè)置的工作路徑中
    [IFlySetting setLogFile:LVL_ALL];
    
    //打開輸出在console的log開關(guān)
    [IFlySetting showLogcat:YES];
    
    //設(shè)置sdk的工作路徑
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    [IFlySetting setLogFilePath:cachePath];
    
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@", APPID];
    [IFlySpeechUtility createUtility:initString];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    [[IFlySpeechUtility getUtility] handleOpenURL:url];
    return YES;
}
ViewController.m里邊寫入正常的無(wú)UI添加方法
@property (nonatomic, strong) IFlySpeechRecognizer *iFlySpeechRecognizer;

創(chuàng)建語(yǔ)音識(shí)別對(duì)象
- (void)MSCSpeech{
    //創(chuàng)建語(yǔ)音識(shí)別對(duì)象
    _iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance];
    //設(shè)置識(shí)別參數(shù)
    //設(shè)置為聽寫模式
    [_iFlySpeechRecognizer setParameter: @"iat" forKey: [IFlySpeechConstant IFLY_DOMAIN]];
    //asr_audio_path 是錄音文件名,設(shè)置value為nil或者為空取消保存,默認(rèn)保存目錄在Library/cache下。
    [_iFlySpeechRecognizer setParameter:@"iat.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
    //去掉逗號(hào)
    [_iFlySpeechRecognizer setParameter:@"0" forKey:[IFlySpeechConstant ASR_PTT]];

    [_iFlySpeechRecognizer setDelegate: self];
    [self.view addSubview:self.speechButton];
    [self.view addSubview:self.speechLabel];
    //[self.view addSubview:self.audioStreamButton];
    //啟動(dòng)識(shí)別服務(wù)
   // [_iFlySpeechRecognizer startListening];
    if (_speechView == nil) {
        _speechView = [MSCVoiceRecordToastContentView new];
        _speechView.center = CGPointMake(self.view.frame.size.width/2, self.speechButton.frame.origin.y-100);
        _speechView.bounds = CGRectMake(0, 0, 120, 120);
        _speechView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
        _speechView.layer.cornerRadius = 6;
        _speechView.hidden = YES;
        [self.view addSubview:_speechView];
    }
    
    
}
當(dāng)然聲明代理,同時(shí)科大訊飛語(yǔ)音delegate的幾個(gè)回調(diào)方法也要寫出來(lái)
//IFlySpeechRecognizerDelegate協(xié)議實(shí)現(xiàn)
//識(shí)別結(jié)果返回代理
- (void) onResults:(NSArray *) results isLast:(BOOL)isLast{
    NSMutableString *resultString = [[NSMutableString alloc] init];
    NSDictionary *dic = results[0];
    for (NSString *key in dic) {
        [resultString appendFormat:@"%@",key];
    }
    NSString * resultFromJson =  [MSCHelper stringFromJson:resultString];
    
    self.speechLabel.text = [NSString stringWithFormat:@"%@%@", self.speechLabel.text,resultFromJson];
   // NSLog(@"當(dāng)前獲取的數(shù)據(jù) :  %@",resultFromJson);
    if ([_iFlySpeechRecognizer isListening]) {
        NSLog(@"正在識(shí)別");
    }
 
    
}
//識(shí)別會(huì)話結(jié)束返回代理
- (void)onError: (IFlySpeechError *) error{
    NSLog(@"錯(cuò)誤原因 : %@",error.errorDesc);
    if (self.speechButton.selected) {
        [_iFlySpeechRecognizer startListening];
    }else{
        [_iFlySpeechRecognizer cancel];
    }
}
//停止錄音回調(diào)
- (void) onEndOfSpeech{
    NSLog(@"結(jié)束錄音");
}
//開始錄音回調(diào)
- (void) onBeginOfSpeech{
    NSLog(@"開始錄音");
}
//音量回調(diào)函數(shù)
- (void) onVolumeChanged: (int)volume{
    [self.speechView updateWithPower:volume];
}
//會(huì)話取消回調(diào)
- (void) onCancel{
    NSLog(@"取消了當(dāng)前會(huì)話");
}
這里科大訊飛語(yǔ)音聽寫功能就接入完畢了,可以嘗試運(yùn)行程序了。但是我個(gè)人感覺看著沒有UI界面的心里難受。所以自己寫了一個(gè)語(yǔ)音音量動(dòng)畫,包括定時(shí)器。使用方法比較簡(jiǎn)單,效果就是上邊看到的效果圖。這兩天網(wǎng)絡(luò)比較差,翻墻老是被卡住,有時(shí)間把Demo給大家放出來(lái),或者有需要的在下方給我留言,
最后編輯于
?著作權(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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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