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

官方文档:http://www.xfyun.cn/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 © 2017年 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 下载:https://github.com/jiangxh1992/XFSpeechDemo

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_厚厚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值