ios项目开发—TTS技术的实现即语音播报(实现方法二)

一、简单说明

 (1)实现思路:

   使用讯飞平台实现文本转语音一般的方法就是上传一段文字到谷歌的接口,需要网络才行,不过现在讯飞开放平台已经开放离线合成与识别了,不过以下代码是需要网络的,离线要钱。同前文一样,我一封装好工具类了,支持64。

 

  (2)需要导入的框架

需要添加系统的库

 AdSupport

 AddressBook

 QuartzCore

 SystemCOnfiguration

 AudioToolBox

 libz

 讯飞提供的库

 添加iflyMSC的时候需要注意,不要拖入工程,应该采用addfiles添加文件

 iflyMSC.frameWork

 

二、实现代码

  TXTexttoSpeechTTS.h文件代码

  

 1 //  Created by 鑫 on 14-10-27.
 2 //  Copyright (c) 2014年 梁镋鑫. All rights reserved.
 3 //
 4 /*
 5  需要添加系统的库
 6  AdSupport
 7  AddressBook
 8  QuartzCore
 9  SystemCOnfiguration
10  AudioToolBox
11  libz
12  讯飞提供的库
13  添加iflyMSC的时候需要注意,不要拖入工程,应该采用addfiles添加文件
14  iflyMSC.frameWork
15  
16  
17  
18  代码示例
19  //读取
20  TXTexttoSpeechTTS *manager=[TXTexttoSpeechTTS  shareManager];
21  [manager playVoice:@"我老婆最漂亮"];
22  
23  
24  */
25 
26 
27 
28 
29 #import <Foundation/Foundation.h>
30 #import "iflyMSC/IFlySpeechSynthesizer.h"
31 #import "iflyMSC/IFlySpeechUtility.h"
32 @interface TXTexttoSpeechTTS : NSObject<IFlySpeechSynthesizerDelegate>
33 {
34      IFlySpeechSynthesizer* iFlySpeechSynthesizer;
35 }
36 @property(nonatomic,copy)void(^onResult)(NSString*);
37 +(id)shareManager;
38 //播放
39 -(void)playVoice:(NSString*)str;
40 @end

 

  TXTexttoSpeechTTS.m.文件代码

  1 //  Created by 鑫 on 14-10-27.
  2 //  Copyright (c) 2014年 梁镋鑫. All rights reserved.
  3 //
  4 
  5 #import "TXTexttoSpeechTTS.h"
  6 #import "iflyMSC/IFlySpeechSynthesizer.h"
  7 @implementation TXTexttoSpeechTTS
  8 static TXTexttoSpeechTTS *manager=nil;
  9 -(id)init
 10 {
 11     if (self=[super init]) {
 12         
 13     }
 14     return self;
 15 }
 16 +(id)shareManager{
 17     static dispatch_once_t onceToken;
 18     dispatch_once(&onceToken, ^{
 19         if (manager==nil) {
 20             manager=[[self alloc]init];
 21         }
 22     });
 23     return manager;
 24 }
 25 //播放语音
 26 -(void)playVoice:(NSString*)str{
 27     NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"12345678"]; [IFlySpeechUtility createUtility:initString];
 28     //    iFlySpeechSynthesizer = [IFlySpeechSynthesizer createWithParams:@"appid=52bbb432" delegate:self];
 29     iFlySpeechSynthesizer =[IFlySpeechSynthesizer sharedInstance];
 30     iFlySpeechSynthesizer.delegate = self;
 31     
 32     
 33     
 34     //    // 设置语音合成的参数-(BOOL) setParameter:(NSString *) value forKey:(NSString*)key;
 35     [ iFlySpeechSynthesizer setParameter:@"speed" forKey:@"300"];//合成的语速,取值范围 0~100
 36     [ iFlySpeechSynthesizer setParameter:@"volume" forKey:@"50"];//合成的音量;取值范围 0~100
 37     //发音人,默认为”xiaoyan”;可以设置的参数列表可参考个 性化发音人列表;
 38     [ iFlySpeechSynthesizer setParameter:@"voice_name" forKey:@"xiaoyan"];
 39     [ iFlySpeechSynthesizer setParameter:@"sample_rate" forKey:@"8000"];//音频采样率,目前支持的采样率有 16000 和 8000;
 40     
 41     [ iFlySpeechSynthesizer startSpeaking:str];
 42     
 43 }
 44 
 45 #pragma mark 语音播放的代理函数
 46 /** 开始合成回调 */
 47 - (void) onSpeakBegin{
 48     
 49 }
 50 /** 缓冲进度回调
 51  @param progress 缓冲进度,0-100
 52  @param message 附件信息,此版本为nil
 53  */
 54 - (void) onBufferProgress:(int) progress message:(NSString *)msg{
 55     
 56 }
 57 
 58 /** 播放进度
 59  
 60  @param progress 播放进度,0-100
 61  */
 62 - (void) onSpeakProgress:(int) progress{
 63     
 64 }
 65 
 66 /** 暂停播放 */
 67 - (void) onSpeakPaused{
 68     
 69 }
 70 
 71 /** 恢复播放 */
 72 - (void) onSpeakResumed{
 73     
 74 }
 75 
 76 /** 结束回调
 77  
 78  当整个合成结束之后会回调此函数
 79  @param error 错误码
 80  */
 81 
 82 - (void) onCompleted:(IFlySpeechError*) error{
 83     
 84 }
 85 /** 正在取消
 86  
 87  当调用`cancel`之后会回调此函数
 88  */
 89 - (void) onSpeakCancel
 90 {
 91     
 92 }
 93 
 94 
 95 
 96 
 97 
 98 
 99 
100 
101 
102 @end

 用法示例:

-(void)playVedio:(id)btn{
    
    TXTexttoSpeechTTS *manager=[TXTexttoSpeechTTS shareManager];
    [manager playVoice:[NSString stringWithFormat:@"今天%@ 的天气,%@,         温度%@度,     湿度%@,       %@,%@ ,  ,  ,极简天气,为 您 播 报",_theCity.cityName,_theCity.fuWeatherArry[0][@"weather"],_theCity.weatherDic[@"temp"],_theCity.weatherDic[@"SD"],_theCity.weatherDic[@"WD"],_theCity.weatherDic[@"WS"]]];

}

 

 

原文地址:https://www.cnblogs.com/asd5551680/p/4180623.html