Reputation: 5384
I try using speech recognition as below
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self startRecognizer];
}
- (void)startRecognizer {
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
if (status == SFSpeechRecognizerAuthorizationStatusAuthorized)
{
SFSpeechRecognizer *sf =[[SFSpeechRecognizer alloc] init];
NSURL *mp3Path = [[NSBundle bundleForClass:[self class]] URLForResource:@"test" withExtension:@"mp3"];
SFSpeechURLRecognitionRequest *speechRequest = [[SFSpeechURLRecognitionRequest alloc]initWithURL:mp3Path];
[sf recognitionTaskWithRequest:speechRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
NSString * translatedString = [[[result bestTranscription] formattedString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@",translatedString);
}];
}
}];
}
Upvotes: 3
Views: 1926
Reputation: 27438
add NSSpeechRecognitionUsageDescription
key in your info.plist
and run this project in device if you are testing in simulator
. You need ios real device running ios10
to test this!
You can refer hackingwithswift's arcticle for more details!
Upvotes: 1