有關整合科大訊飛sdk的語音(二)不帶語音的介面
不帶語音的就需要自定義語音識別時播放的動畫.在呼叫的時候我們需要先判斷麥克風的許可權.
- (void)getUserMicrophonePermissions{
int flag;
AVAuthorizationStatus authStatus = [AVCaptureDeviceauthorizationStatusForMediaType:AVMediaTypeAudio];
if (authStatus ==AVAuthorizationStatusNotDetermined) {
// 沒有詢問是否開啟麥克風
[[AVAudioSessionsharedInstance] requestRecordPermission:^(BOOL granted) {
// CALL YOUR METHOD HERE - as this assumes being called only once from user interacting with permission alert! //第一次詢問使用者是否進行授權
if (granted) {
// Microphone enabled code
[self speechSpeakAndWrite];
}
else
// Microphone disabled code
[self showSetAlertView];
}
}];
}elseif (authStatus ==AVAuthorizationStatusRestricted || authStatus==AVAuthorizationStatusDenied){
//未授權,家長限制 玩家未授權
[selfshowSetAlertView];
}elseif (authStatus ==AVAuthorizationStatusAuthorized){
// 玩家授權
[selfspeechSpeakAndWrite];
}
}
//提示使用者進行麥克風使用授權
- (void)showSetAlertView {
UIAlertController *alertVC = [UIAlertControlleralertControllerWithTitle:@"麥克風許可權未開啟" message:@"麥克風許可權未開啟,請進入系統【設定】>【隱私】>【麥克風】中開啟開關,開啟麥克風功能" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"知道了" style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {
}];
dispatch_async(dispatch_get_main_queue(), ^{
//這裡面就可以實現有關當沒有開啟許可權時需要呈現的頁面
});
//下面的方法是如果需要跳轉,則直接跳轉設定 由於在點選去跳轉設定後,回來會重新啟動APP,而我也暫時沒有想到一些解決辦法,有些產品並不接受這,比如我們的產品,所以我就仿微信,直接不讓跳轉,一了百了!
// UIAlertAction *setAction = [UIAlertAction actionWithTitle:@"去設定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// //跳入當前App設定介面
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
// }];
[alertVC addAction:cancelAction];
[selfpresentViewController:alertVC animated:YEScompletion:nil];
}
@property (nonatomic,strong) IFlySpeechRecognizer *iFlySpeechRecognizer;
- (void)speechSpeakAndWrite{
if (_iFlySpeechRecognizer ==nil) {
[self initRecognizer];
}
BOOL ret = [_iFlySpeechRecognizerstartListening];
if (ret) {
//啟動識別服務成功
dispatch_async(dispatch_get_main_queue(), ^{
//因為語音的sdk是在多執行緒裡面進行語音識別的,這就需要我們在主執行緒裡面進行UI的顯示,否則UI的頁面是不會顯示出來的,好多sdk都是這樣的,需注意這點
});
}else{
//啟動識別服務失敗
[MBProgressHUDshowMessage:@"啟動識別服務失敗"];
}
}
- (void)initRecognizer{
if (_iFlySpeechRecognizer ==nil) {
_iFlySpeechRecognizer = [IFlySpeechRecognizersharedInstance];
}
if (_iFlySpeechRecognizer !=nil) {
[_iFlySpeechRecognizersetParameter:@""forKey:[IFlySpeechConstantPARAMS]];
//設定聽寫模式
[_iFlySpeechRecognizersetParameter:@"iat"forKey:[IFlySpeechConstantIFLY_DOMAIN]];
//設定最長錄音時間
[_iFlySpeechRecognizersetParameter:@"10000"forKey:[IFlySpeechConstantSPEECH_TIMEOUT]];
//設定後端點 sdk中的後端點是3秒,意思就是3秒沒有接收到資訊,線上識別的sdk將停止接收
[_iFlySpeechRecognizersetParameter:@"3000"forKey:[IFlySpeechConstantVAD_EOS]];
//設定前端點 sdk中的前端點是3秒,意思就是5秒沒有接收到資訊,將停止接收說話
[_iFlySpeechRecognizersetParameter:@"5000"forKey:[IFlySpeechConstantVAD_BOS]];
//網路等待時間
[_iFlySpeechRecognizersetParameter:@"10000"forKey:[IFlySpeechConstantNET_TIMEOUT]];
//設定取樣率,推薦使用16K
[_iFlySpeechRecognizersetParameter:@"16000"forKey:[IFlySpeechConstantSAMPLE_RATE]];
//設定語言
[_iFlySpeechRecognizersetParameter:@"zh_cn"forKey:[IFlySpeechConstantLANGUAGE]];
//設定方言
[_iFlySpeechRecognizersetParameter:@"mandarin"forKey:[IFlySpeechConstantACCENT]];
//設定是否返回標點符號
[_iFlySpeechRecognizersetParameter:@"0"forKey:[IFlySpeechConstantASR_PTT]];
//設定資料返回格式
[_iFlySpeechRecognizersetParameter:@"plain"forKey:[IFlySpeechConstantRESULT_TYPE]];
//設定音訊來源為麥克風
[_iFlySpeechRecognizersetParameter:IFLY_AUDIO_SOURCE_MICforKey:@"audio_source"];
//設定聽寫結果格式為json
[_iFlySpeechRecognizersetParameter:@"plain"forKey:[IFlySpeechConstantRESULT_TYPE]];
//保存錄音檔案,儲存在sdk工作路徑中,如未設定工作路徑,則預設儲存在library/cache下
[_iFlySpeechRecognizersetParameter:@"asr.pcm"forKey:[IFlySpeechConstantASR_AUDIO_PATH]];
[_iFlySpeechRecognizersetDelegate:self];
}
}
#pragma mark - IFlySpeechRecognizerDelegate
注意在有介面的代理方法裡面是 - (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast
這裡是onResults不一樣
- (void) onResults:(NSArray *) results isLast:(BOOL)isLast
{
NSMutableString *result = [[NSMutableStringalloc] init];
NSDictionary *dic = [resultsobjectAtIndex:0];
NSLog(@"result279 = %@",result);
for (NSString *keyin dic) {
[result appendFormat:@"%@",key];
}
if (!isLast) {
_content = result;
}else{
NSLog(@"result248 = %@",_content);
}
}
接下來我說有關語音合成的...