1, register to fly account, apply for AppID (Note select iOS platform) 2, load the required class Library 3, import the required class library file header 4, call the requested AppID and the required functions, complete the speech synthesis (need to refer to the Official SDK file) detailed steps:
First, to the Iflytek website Registration account (http://open.voicecloud.cn/), and create an application to get AppID, download the SDK file
Second, the code implementation API call
1. Create a new project with Xcode (I'm using Xcode 5.1 here), and then add the class library you want to use in your project. One of them is the voice of the class library iflymsc, in the download of the SDK file, import on the line. When importing should pay attention to copy the Iflymsc class library to your project directory, otherwise the consequence is very serious!
2. After the class library is completed, add the header files to be used in the project under construction.
MainViewController.h
| 12 |
#import <UIKit/UIKit.h>#import "iflyMSC/IFlySpeechSynthesizerDelegate.h" |
Mainviewcontroller.m
| 1234567 |
#import "MainViewController.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" |
3. After completing these preparations, the next step is to work on the heap code. For convenience, the author uses only two controls: a Uitextfield, a uibutton, and then gives the two controls a outlet and action connection.
MainViewController.h
| 12345678910111213 |
#import <uikit/uikit.h> #import "Iflymsc/iflyspeechsynthesizerdelegate.h" //introduces speech synthesis class @ class Iflyspeechsynthesizer; @ class Iflydatauploader; //Note to add a speech synthesis agent @interface mainviewcontroller:uiviewcontroller<iflyspeechsynthesizerdelegate> //declares an object for speech synthesis @property (nonatomic, Strong) Iflyspeechsynthesizer *iflyspeechsynthesizer; @property (strong, nonatomic) Iboutlet Uitextfield *content; -(ibaction) Start: (ID) sender; @end |
Mainviewcontroller.m
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677787980818283848586878889909192939495969798 |
#import "MainViewController.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 MainViewController () @end@implementation MainViewController- (void)viewDidLoad{ [super viewDidLoad]; //通过appid连接讯飞语音服务器,把@"53b5560a"换成你申请的appid NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",@"53b5560a",@"20000"]; //所有服务启动前,需要确保执行createUtility [IFlySpeechUtility createUtility:initString]; //创建合成对象,为单例模式 _iFlySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance]; _iFlySpeechSynthesizer.delegate = self; //设置语音合成的参数 //合成的语速,取值范围 0~100 [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant SPEED]]; //合成的音量;取值范围 0~100 [_iFlySpeechSynthesizer setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]]; //发音人,默认为”xiaoyan”;可以设置的参数列表可参考个性化发音人列表 [_iFlySpeechSynthesizer setParameter:@"xiaoyan" 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 |
4. The above agent method can not be written in fact, but the official instructions are required to add. If an error occurs during the run, you can check the error code list of the developer documentation to find the appropriate error.
Speech Synthesis for iOS development (Iflytek)