语音合成

语音合成实现了人机交互,实现了有听和讲能力的交互系统;我用的是科大讯飞的第三方类具体如下(我用的xcode6):

在ViewController.h  具体代码如下:

#import <UIKit/UIKit.h>
#import "iflyMSC/IFlySpeechSynthesizerDelegate.h"
//引入语音合成类
@class IFlySpeechSynthesizer;
@class IFlyDataUploader;
//注意要添加语音合成代理
@interface ViewController : UIViewController<IFlySpeechSynthesizerDelegate>
//声明语音合成的对象
@property (nonatomic, strong) IFlySpeechSynthesizer *iFlySpeechSynthesizer;
@property (weak, nonatomic) IBOutlet UITextView *content;

- (IBAction)Start:(id)sender;
@end


在ViewController.m  具体代码如下:

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVAudioSession.h>
#import <AudioToolbox/AudioSession.h>
#import "iflyMSC/IFlySpeechConstant.h"
#import "iflyMSC/IFlySpeechUtility.h"
#import "iflyMSC/IFlySpeechSynthesizer.h"
@interface ViewController ()

@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _content.layer.borderColor = [[UIColor colorWithRed:230.0/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:1.0]CGColor];
    _content.layer.borderWidth = 3.0;
    _content.layer.cornerRadius = 8.0f;
    [_content.layer setMasksToBounds:YES];
    
    
    //通过appid连接讯飞语音服务器,把@""换成你申请的appid
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",@"562d9d5d",@"20000"];
    //所有服务启动前,需要确保执行createUtility
    [IFlySpeechUtility createUtility:initString];
    
    //创建合成对象,为单例模式
    _iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];
    _iFlySpeechSynthesizer.delegate = self;
    
    //设置语音合成的参数
    //合成的语速,取值范围 0~100

    [_iFlySpeechSynthesizer setParameter:@"30" forKey:[IFlySpeechConstant SPEED]];
    //合成的音量;取值范围 0~100
    [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]];
    //发音人,默认为”xiaoyan”;可以设置的参数列表可参考个性化发音人列表
    [_iFlySpeechSynthesizer setParameter:@"xiaoxin" forKey:[IFlySpeechConstant VOICE_NAME]];
    //音频采样率,目前支持的采样率有 16000 和 8000
    [_iFlySpeechSynthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
    asr_audio_path保存录音文件路径,如不再需要,设置value为nil表示取消,默认目录是documents
    [_iFlySpeechSynthesizer setParameter:@"tts.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];
    
    //隐藏键盘,点击空白处
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGr];
 
}
-(void)viewTapped:(UITapGestureRecognizer*)tapGr
{
    [self.content resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)Start:(id)sender {
    //启动合成会话
    [_iFlySpeechSynthesizer startSpeaking:self.content.text];
    
}
#pragma mark - IFlySpeechSynthesizerDelegate
//开始播放
- (void) onSpeakBegin
{
    
}
//缓冲进度
- (void) onBufferProgress:(int) progress message:(NSString *)msg
{
    NSLog(@"bufferProgress:%d,message:%@",progress,msg);
}

//播放进度
- (void) onSpeakProgress:(int) progress
{
    NSLog(@"play progress:%d",progress);
}
//暂停播放
- (void) onSpeakPaused
{
    
}
//恢复播放
- (void) onSpeakResumed
{
    
}
//结束回调
- (void) onCompleted:(IFlySpeechError *) error
{
    
}
@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值