百度云语音合成

上篇介绍了百度云语音识别的功能,接下来开始实现百度云语音合成的功能。

接上篇:百度云语音识别

一,配置环境,导入依赖的库文件

二,

#import "BDSSpeechSynthesizer.h"

//遵循代理
@interface ViewController ()<BDSSpeechSynthesizerDelegate>
//初始化控件
@property(nonatomic,strong)UIButton *voiceSynthesisButton;
@property(nonatomic,strong) UITextView *textView;

self.voiceSynthesisButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 150, 375, 40)];
    self.voiceSynthesisButton.backgroundColor = [UIColor redColor];
    [self.voiceSynthesisButton setTitle:@"开始语音合成" forState:UIControlStateNormal];
    [self.voiceSynthesisButton addTarget:self action:@selector(startSynthesis:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.voiceSynthesisButton];
    
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 220, 375,200)];
    textView.backgroundColor = [UIColor yellowColor];
    textView.font = [UIFont systemFontOfSize:17.0];
    [self.view addSubview:textView];
    self.textView = textView;

//初始化百度语音合成
- (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)startSynthesis:(UIButton *)btn{
    NSString *contentStr = self.textView.text;
    //朗读内容
    [self.bdSpeech speakSentence:contentStr withError:nil];
}

三,演示地址:百度云语音识别,合成   密码:ebpv

猜你喜欢

转载自blog.csdn.net/ZHUTAN_123/article/details/81808395