·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> IOS开发学习笔记(一)语音合成

IOS开发学习笔记(一)语音合成

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

      现在语音服务越来越热,我们平时使用的很多软件都带有语音合成和识别功能,用起来也很方便。说到语音服务,Google和微软都提供过API接口,不过笔者要介绍的是国内的智能语音技术提供商---科大讯飞。之前看过一个比较Google、微软和科大讯飞语音识别引擎的博文(http://fqctyj.blog.163.com/blog/static/70843455201361955322797/),有兴趣可以去看看。笔者接触语音服务的时间也不长,对语音服务也不是很了解,但是拆解过科大讯飞的Demo,对语音服务的程序使用还是知道的。这次只整理了语音合成的代码,关于语音识别和其他的下次再发,废话完了进入正题。

如何实现语音合成呢?

1、注册讯飞账号,申请APPID(注意选择IOS平台) 2、加载所需要的类库 3、导入所需要的类库文件头 4、调用申请的APPID以及所需函数,完成语音合成(需要参考官方给出的SDK文件)   详细步骤:

一、首先到科大讯飞官网注册账号(http://open.voicecloud.cn/),并创建应用获取appid,下载sdk文件

 

二、代码实现api调用

1.先用xcode(我这里使用的是xcode 5.1)新建好一个项目,然后在项目添加要用的类库。其中有一个是讯飞语音的类库iflyMSC,在下载的sdk文件里有,导入就行了。导入的时候要注意把iflyMSC类库拷贝到你的工程目录里,不然后果很严重!

 

2.导完类库之后,在建好的工程里添加好要用的头文件。

MainViewController.h

1 #import <UIKit/UIKit.h>
2 #import "iflyMSC/IFlySpeechSynthesizerDelegate.h"

MainViewController.m

1 #import "MainViewController.h"
2 #import <QuartzCore/QuartzCore.h>
3 #import <AVFoundation/AVAudiosession.h>
4 #import <AudioToolbox/AudioSession.h>
5 #import "iflyMSC/IFlySpeechConstant.h"
6 #import "iflyMSC/IFlySpeechUtility.h"
7 #import "iflyMSC/IFlySpeechSynthesizer.h"

3.完成这些准备工作之后,接下来就是堆代码的工作了。为了方便,笔者只用了两个控件:一个UITextField、一个UIButton,然后给这两个控件分别做一个Outlet和Action连接。

MainViewController.h

 1 #import <UIKit/UIKit.h>
 2 #import "iflyMSC/IFlySpeechSynthesizerDelegate.h"
 3 //引入语音合成类
 4 @class IFlySpeechSynthesizer;
 5 @class IFlyDataUploader;
 6 //注意要添加语音合成代理
 7 @interface MainViewController : UIViewController<IFlySpeechSynthesizerDelegate>
 8 //声明语音合成的对象
 9 @PRoperty (nonatomic, strong) IFlySpeechSynthesizer *iFlySpeechSynthesizer;
10 @property (strong, nonatomic) IBOutlet UITextField *content;
11 
12 - (IBAction)Start:(id)sender;
13 @end

MainViewController.m

 1 #import "MainViewController.h"
 2 #import <QuartzCore/QuartzCore.h>
 3 #import <AVFoundation/AVAudioSession.h>
 4 #import <AudioToolbox/AudioSession.h>
 5 #import "iflyMSC/IFlySpeechConstant.h"
 6 #import "iflyMSC/IFlySpeechUtility.h"
 7 #import "iflyMSC/IFlySpeechSynthesizer.h"
 8 
 9 @interface MainViewController ()
10 
11 @end
12 
13 @implementation MainViewController
14 
15 - (void)viewDidLoad
16 {
17     [super viewDidLoad];
18     //通过appid连接讯飞语音服务器,把@"53b5560a"换成你申请的appid
19     NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",@"53b5560a",@"20000"];
20     //所有服务启动前,需要确保执行createUtility
21     [IFlySpeechUtility createUtility:initString];
22     
23     //创建合成对象,为单例模式
24     _iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];
25     _iFlySpeechSynthesizer.delegate = self;
26 
27     //设置语音合成的参数
28     //合成的语速,取值范围 0~100
29     [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]];
30     //合成的音量;取值范围 0~100
31     [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]];
32     //发音人,默认为”xiaoyan”;可以设置的参数列表可参考个性化发音人列表
33     [_iFlySpeechSynthesizer setParameter:@"xiaoyan" forKey:[IFlySpeechConstant VOICE_NAME]];
34     //音频采样率,目前支持的采样率有 16000 和 8000
35     [_iFlySpeechSynthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
36     ////asr_audio_path保存录音文件路径,如不再需要,设置value为nil表示取消,默认目录是documents
37     [_iFlySpeechSynthesizer setParameter:"tts.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];
38 
39     //隐藏键盘,点击空白处
40     UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
41     tapGr.cancelsTouchesInView = NO;
42     [self.view addGestureRecognizer:tapGr];  
43 }
44 
45 -(void)viewTapped:(UITapGestureRecognizer*)tapGr
46 {
47     [self.content resignFirstResponder];
48 }
49 
50 - (void)didReceiveMemoryWarning
51 {
52     [super didReceiveMemoryWarning];
53     // Dispose of any resources that can be recreated.
54 }
55 
56 - (IBAction)Start:(id)sender 
57 {
58     //启动合成会话
59     [_iFlySpeechSynthesizer startSpeaking:self.content.text];
60 }
61 
62 #pragma mark - IFlySpeechSynthesizerDelegate
63 //开始播放
64 - (void) onSpeakBegin
65 {
66     
67 }
68 
69 //缓冲进度
70 - (void) onBufferProgress:(int) progress message:(NSString *)msg
71 {
72     NSLog(@"bufferProgress:%d,message:%@",progress,msg);
73 }
74 
75 //播放进度
76 - (void) onSpeakProgress:(int) progress
77 {
78     NSLog(@"play progress:%d",progress);
79 }
80 
81  //暂停播放
82 - (void) onSpeakPaused
83 {
84     
85 }
86 
87 //恢复播放
88 - (void) onSpeakResumed
89 {
90     
91 }
92 
93 //结束回调
94 - (void) onCompleted:(IFlySpeechError *) error
95 {
96     
97 }
98 @end

4.以上的代理方法其实是可以不写的,但是官方给出的说明是需要加上的。若是在运行过程中出现错误,可以查看开发者文档的错误码列表,找出相应的错误。如果大家对以上的讲解还有不懂的地方,请仔细阅读官方提供的sdk文件,里面有详细的说明,也可以给我留言。

PS:若本文有什么错误的地方,还望大家指正留言,我会尽快修改!