iOSの音声認識(SFSpeechRecognizer)をObjective-Cで
7018 ワード
iOSの音声認識をObjective-Cで
Swiftの記事は見つかるが、Objective-Cで書いてある記事がみつからなかったので書いておきます。
Swiftはこちら
http://dev.classmethod.jp/smartphone/iphone/try-ios10-speech-recognizer/
ユーザに許可を求める
hoge.m
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
}];
変数定義
SpeechRecognizerManager.m
@interface SpeechRecognizerManager()
{
SFSpeechRecognizer* speechRecognizer;
SFSpeechAudioBufferRecognitionRequest* speechRequest;
SFSpeechRecognitionTask* recognazationTask;
AVAudioEngine *audioEngine;
}
@end
各種メソッド
SpeechRecognizerManager.m
-(SpeechRecognizerManagerIOS*)init
{
audioEngine = [[AVAudioEngine alloc]init];
if( audioEngine.inputNode ){
[audioEngine.inputNode installTapOnBus:0 bufferSize:1024 format:nil block:^(AVAudioPCMBuffer* buffer, AVAudioTime* when) {
[speechRequest appendAudioPCMBuffer:buffer];
}];
}
speechRecognizer = [[SFSpeechRecognizer alloc]initWithLocale:[NSLocale localeWithLocaleIdentifier:@"ja-JP"]];
speechRecognizer.delegate = self;
speechRequest = [[SFSpeechAudioBufferRecognitionRequest alloc]init];
speechRequest.shouldReportPartialResults = YES;
return self;
}
-(void)start
{
[audioEngine prepare];
[audioEngine startAndReturnError:nil];
recognazationTask = [speechRecognizer recognitionTaskWithRequest:speechRequest resultHandler:^(SFSpeechRecognitionResult *result, NSError * error) {
if( result ){
// ここで認識できた文字列が取得できる
NSLog(@"%@", result.bestTranscription.formattedString);
}
}];
}
-(void)stop
{
[audioEngine stop];
[speechRequest endAudio];
[recognazationTask cancel];
recognazationTask = nil;
}
Author And Source
この問題について(iOSの音声認識(SFSpeechRecognizer)をObjective-Cで), 我々は、より多くの情報をここで見つけました https://qiita.com/ryohorie/items/1e3b9cd78be752ae9cdd著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .