国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOS開發學習筆記(一)語音合成

2019-11-14 20:16:05
字體:
來源:轉載
供稿:網友

      現在語音服務越來越熱,我們平時使用的很多軟件都帶有語音合成和識別功能,用起來也很方便。說到語音服務,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 @end12 13 @implementation MainViewController14 15 - (void)viewDidLoad16 {17     [super viewDidLoad];18     //通過appid連接訊飛語音服務器,把@"53b5560a"換成你申請的appid19     NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",@"53b5560a",@"20000"];20     //所有服務啟動前,需要確保執行createUtility21     [IFlySpeechUtility createUtility:initString];22     23     //創建合成對象,為單例模式24     _iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];25     _iFlySpeechSynthesizer.delegate = self;26 27     //設置語音合成的參數28     //合成的語速,取值范圍 0~10029     [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]];30     //合成的音量;取值范圍 0~10031     [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]];32     //發音人,默認為”xiaoyan”;可以設置的參數列表可參考個性化發音人列表33     [_iFlySpeechSynthesizer setParameter:@"xiaoyan" forKey:[IFlySpeechConstant VOICE_NAME]];34     //音頻采樣率,目前支持的采樣率有 16000 和 800035     [_iFlySpeechSynthesizer setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];36     ////asr_audio_path保存錄音文件路徑,如不再需要,設置value為nil表示取消,默認目錄是documents37     [_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*)tapGr46 {47     [self.content resignFirstResponder];48 }49 50 - (void)didReceiveMemoryWarning51 {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 - IFlySpeechSynthesizerDelegate63 //開始播放64 - (void) onSpeakBegin65 {66     67 }68 69 //緩沖進度70 - (void) onBufferProgress:(int) progress message:(NSString *)msg71 {72     NSLog(@"bufferProgress:%d,message:%@",progress,msg);73 }74 75 //播放進度76 - (void) onSpeakProgress:(int) progress77 {78     NSLog(@"play progress:%d",progress);79 }80 81  //暫停播放82 - (void) onSpeakPaused83 {84     85 }86 87 //恢復播放88 - (void) onSpeakResumed89 {90     91 }92 93 //結束回調94 - (void) onCompleted:(IFlySpeechError *) error95 {96     97 }98 @end

4.以上的代理方法其實是可以不寫的,但是官方給出的說明是需要加上的。若是在運行過程中出現錯誤,可以查看開發者文檔的錯誤碼列表,找出相應的錯誤。如果大家對以上的講解還有不懂的地方,請仔細閱讀官方提供的sdk文件,里面有詳細的說明,也可以給我留言。

PS:若本文有什么錯誤的地方,還望大家指正留言,我會盡快修改!

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄大仙区| 石渠县| 太和县| 黎川县| 鄱阳县| 任丘市| 田东县| 泰和县| 溧阳市| 滨州市| 石家庄市| 永平县| 陇南市| 松江区| 新丰县| 西华县| 景德镇市| 博野县| 观塘区| 三原县| 汕尾市| 平顶山市| 沙雅县| 淮滨县| 铜陵市| 玛多县| 清流县| 神池县| 益阳市| 安国市| 延吉市| 萨嘎县| 恭城| 柳河县| 永清县| 延川县| 土默特左旗| 江山市| 罗甸县| 舒兰市| 扶风县|