IOS開發學習筆記--語音合成(科大訊飛)

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   strong   

      現在Voice Messaging Service越來越熱,我們平時使用的很多軟體都帶有語音合成和識別功能,用起來也很方便。說到Voice Messaging Service,Google和微軟都提供過API介面,不過筆者要介紹的是國內的智能語音技術供應商---科大訊飛。之前看過一個比較Google、微軟和科大訊飛語音辨識引擎的博文(http://fqctyj.blog.163.com/blog/static/70843455201361955322797/),有興趣可以去看看。筆者接觸Voice Messaging Service的時間也不長,對Voice Messaging Service也不是很瞭解,但是拆解過科大訊飛的Demo,對Voice Messaging Service的程式使用還是知道的。這次只整理了語音合成的代碼,關於語音辨識和其他的下次再發,廢話完了進入正題。

如何?語音合成呢?

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串連訊飛Voice Messaging Service器,把@"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:若本文有什麼錯誤的地方,還望大家指正留言,我會儘快修改!

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.