1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 【Demo】iOS平台上的讯飞语音识别语音合成开发

【Demo】iOS平台上的讯飞语音识别语音合成开发

时间:2023-09-25 17:42:19

相关推荐

【Demo】iOS平台上的讯飞语音识别语音合成开发

官方文档:/doccenter/iOS

目前开放的服务:

准备工作

需要到讯飞官网注册一个开发账号,注册后登录并创建一个新的应用,添加需要的服务(语音听写、语音合成等等),应用创建后可以得到一个Appid,这个要在开发中初始化讯飞语音应用中用到。

工程中找到Targets->Build Phases->Link Binary With Libraries,添加下面的库,注意iflyMSC.framework下载后一定要保证导入到工程目录下,而不能只是个路径引用否则会报错。另外需要在Build Settings的Build Options中关闭BitCode。

在需要的地方引入讯飞语音库头文件,当然可以根据需要只引入某种服务的头文件

#import <iflyMSC/iflyMSC.h> // 引入讯飞语音库

#ifndef MSC_IFlyMSC_h#define MSC_IFlyMSC_h#import "IFlyAudioSession.h"#import "IFlyContact.h"#import "IFlyDataUploader.h"#import "IFlyDebugLog.h"#import "IFlyISVDelegate.h"#import "IFlyISVRecognizer.h"#import "IFlyRecognizerView.h"#import "IFlyRecognizerViewDelegate.h"#import "IFlyResourceUtil.h"#import "IFlySetting.h"#import "IFlySpeechConstant.h"#import "IFlySpeechError.h"#import "IFlySpeechEvaluator.h"#import "IFlySpeechEvaluatorDelegate.h"#import "IFlySpeechEvent.h"#import "IFlySpeechRecognizer.h"#import "IFlySpeechRecognizerDelegate.h"#import "IFlySpeechSynthesizer.h"#import "IFlySpeechSynthesizerDelegate.h"#import "IFlySpeechUnderstander.h"#import "IFlySpeechUtility.h"#import "IFlyTextUnderstander.h"#import "IFlyUserWords.h"#import "IFlyPcmRecorder.h"#import "IFlyVoiceWakeuper.h"#import "IFlyVoiceWakeuperDelegate.h"#endif

使用Appid初始化讯飞应用,在应用启动的地方使用Appid初始化语音应用,这里放在了AppDelegate应用代理文件下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"587f6174"];[IFlySpeechUtility createUtility:initString];return YES;}

语音合成和语音测评示例

这里因为要使用语音合成和测评功能所以先测试了这两个,其他的类似,可参考官方文档:

应用启动后会有‘小燕’的声音读出:”Hello, this is xiaoyan!”应用启动后测评”Today is a sunny day!”的发音

先要使用Appid初始化:

//// ViewController.m// XFDemo//// Created by Xinhou Jiang on 4/3/17.// Copyright © Xinhou Jiang. All rights reserved.//#import "ViewController.h"#import <iflyMSC/iflyMSC.h> // 引入讯飞语音库@interface ViewController ()<IFlySpeechSynthesizerDelegate,IFlySpeechEvaluatorDelegate> { // 语音代理协议}@property (nonatomic, strong)IFlySpeechSynthesizer *iFlySpeechSynthesizer; // 定义语音合成对象@property (nonatomic, strong)IFlySpeechEvaluator *iFlySpeechEvaluator; // 定义语音测评对象@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// 1.开始合成说话//[self.iFlySpeechSynthesizer startSpeaking:@"Hello, this is xiaoyan!"];// 2.开始语音测评NSData *textData = [@"Today is a sunny day!" dataUsingEncoding:NSUTF8StringEncoding];[self.iFlySpeechEvaluator startListening:textData params:nil];}#pragma -mark 语音合成/*** 懒加载getter方法*/- (IFlySpeechSynthesizer *)iFlySpeechSynthesizer {if(!_iFlySpeechSynthesizer) {// 初始化语音合成_iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];_iFlySpeechSynthesizer.delegate = self;// 语速【0-100】[_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]];// 音量【0-100】[_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]];// 发音人【小燕:xiaoyan;小宇:xiaoyu;凯瑟琳:catherine;亨利:henry;玛丽:vimary;小研:vixy;小琪:vixq;小峰:vixf;小梅:vixl;小莉:vixq;小蓉(四川话):vixr;小芸:vixyun;小坤:vixk;小强:vixqa;小莹:vixying;小新:vixx;楠楠:vinn;老孙:vils】[_iFlySpeechSynthesizer setParameter:@"xiaoyan" forKey:[IFlySpeechConstant VOICE_NAME]];// 音频采样率【8000或16000】[_iFlySpeechSynthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];// 保存音频路径(默认在Document目录下)[_iFlySpeechSynthesizer setParameter:@"tts.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];}return _iFlySpeechSynthesizer;}/*** 合成结束*/- (void)onCompleted:(IFlySpeechError *)error {NSLog(@"合成结束!");}/*** 合成开始*/- (void)onSpeakBegin {NSLog(@"合成开始!");}/*** 合成缓冲进度【0-100】*/- (void)onBufferProgress:(int)progress message:(NSString *)msg {NSLog(@"合成缓冲进度:%d/100",progress);}/*** 合成播放进度【0-100】*/- (void)onSpeakProgress:(int)progress beginPos:(int)beginPos endPos:(int)endPos {NSLog(@"合成播放进度:%d/100",progress);}#pragma -mark 语音测评/*** 懒加载getter方法*/- (IFlySpeechEvaluator *)iFlySpeechEvaluator {if (!_iFlySpeechEvaluator) {// 初始化语音测评_iFlySpeechEvaluator = [IFlySpeechEvaluator sharedInstance];_iFlySpeechEvaluator.delegate = self;// 设置测评语种【中文:zh_cn,中文台湾:zh_tw,美英:en_us】[_iFlySpeechEvaluator setParameter:@"en_us" forKey:[IFlySpeechConstant LANGUAGE]];// 设置测评题型【read_syllable(英文评测不支持):单字;read_word:词语;read_sentence:句子;read_chapter(待开放):篇章】[_iFlySpeechEvaluator setParameter:@"read_sentence" forKey:[IFlySpeechConstant ISE_CATEGORY]];// 设置试题编码类型[_iFlySpeechEvaluator setParameter:@"utf-8" forKey:[IFlySpeechConstant TEXT_ENCODING]];// 设置前、后端点超时【0-10000(单位ms)】[_iFlySpeechEvaluator setParameter:@"10000" forKey:[IFlySpeechConstant VAD_BOS]]; // 默认5000ms[_iFlySpeechEvaluator setParameter:@"1000" forKey:[IFlySpeechConstant VAD_EOS]]; // 默认1800ms// 设置录音超时,设置成-1则无超时限制(单位:ms,默认30000)[_iFlySpeechEvaluator setParameter:@"5000" forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];// 设置结果等级,不同等级对应不同的详细程度【complete:完整 ;plain:简单】[_iFlySpeechEvaluator setParameter:@"" forKey:[IFlySpeechConstant ISE_RESULT_LEVEL]];}return _iFlySpeechEvaluator;}/*** 开始说话回调*/- (void)onBeginOfSpeech {NSLog(@"开始说话...");}/*** 说话结束回调*/- (void)onEndOfSpeech {NSLog(@"说话结束...");}/*** 测评结果*/- (void)onResults:(NSData *)results isLast:(BOOL)isLast {NSLog(@"测评结果:%@",results);}/*** 出错回调*/- (void)onError:(IFlySpeechError *)errorCode {NSLog(@"语音测评出错:%@",errorCode);}/*** 音量变化回调*/- (void)onVolumeChanged:(int)volume buffer:(NSData *)buffer {NSLog(@"音量变化...");}/*** 取消*/- (void)onCancel {NSLog(@"正在取消...");}@end

Demo 下载:/jiangxh1992/XFSpeechDemo

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。