背景
目前语音功能比较出名的就是百度、讯飞语音,当然还有其他平台的语音功能,比如微信之类的。
目前我只用过百度、讯飞的语音,具体是讯飞的语音识别和百度的语音合成。很奇葩是吧,为啥要分开来用。要么就选讯飞的语音识别+语音合成,要么就选讯飞的语音识别+语音合成。老板要求的,这个理由够不够,哈哈哈~
回归正题,百度的语音合成,建个新工程再玩一下。
一、百度语音开放平台注册应用
百度语音开放平台:
百度语音-永久免费智能语音开放平台
二、Xcode导入相应的库
你点击你创建的应用,里面有对应的SDK
供你选择。当处创建应用的时候,由于你只选了语音合成,所以只能下载语音合成的SDK
。TTS
就是语音合成。
导入必备的的库,并且把TTS对应的文件也拖进来。
三、开始使用TTS
参考代码:
//
// ViewController.m
// BDSpeechSynthesis
//
// Created by HZhenF on 2017/9/21.
// Copyright © 2017年 GZHYTechnology. All rights reserved.
//
#import "ViewController.h"
#import "BDSSpeechSynthesizer.h"
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
NSString *APP_ID = @"10162806";
NSString *API_KEY = @"BSyQC2PPqSFsBbRjEZXXrRZe";
NSString *SECRET_KEY = @"9307853c7104ffc3e9cd0922b7f38d99";
@interface ViewController ()<BDSSpeechSynthesizerDelegate>
@property(nonatomic,strong) UITextView *textView;
@property(nonatomic,strong) BDSSpeechSynthesizer *BDSpeech;
@end
@implementation ViewController
-(BDSSpeechSynthesizer *)BDSpeech
{
if (!_BDSpeech) {
//设置、获取日志级别
[BDSSpeechSynthesizer setLogLevel:BDS_PUBLIC_LOG_VERBOSE];
_BDSpeech = [BDSSpeechSynthesizer sharedInstance];
//设置合成器代理
[_BDSpeech setSynthesizerDelegate:self];
//为在线合成设置认证信息
[_BDSpeech setApiKey:API_KEY withSecretKey:SECRET_KEY];
//设置语调
[_BDSpeech setSynthParam:[NSNumber numberWithInt:4] forKey:BDS_SYNTHESIZER_PARAM_PITCH];
//设置语速
[_BDSpeech setSynthParam:[NSNumber numberWithInt:5] forKey:BDS_SYNTHESIZER_PARAM_SPEED];
}
return _BDSpeech;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self configureControls];
//初始化百度语音
[self BDSpeech];
}
-(void)configureControls
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(100, 100, 100, 50);
[btn setTitle:@"点击播放" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
CGFloat W = 300;
CGFloat H = 200;
CGFloat X = 0.5*(ScreenW - W);
CGFloat Y = CGRectGetMaxY(btn.frame) + 30;
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(X, Y, W,H)];
textView.backgroundColor = [UIColor yellowColor];
textView.font = [UIFont systemFontOfSize:17.0];
[self.view addSubview:textView];
self.textView = textView;}
-(void)btnAction
{
NSString *contentStr = self.textView.text;
//朗读内容
[self.BDSpeech speakSentence:contentStr withError:nil];
}
@end