I am trying to perform cont. speech recognition using AVCapture
on iOS 10 beta. I have setup captureOutput(...)
to continuously get CMSampleB
I have success to use the SFSpeechRecognizer in continuous. The main point is to use AVCaptureSession to capture audio and transfer to SpeechRecognizer. Sorry I am poor in Swift,so just the ObjC version.
Here is my sample code (leave out some UI code,some important has marked):
@interface ViewController ()
@property (nonatomic, strong) AVCaptureSession *capture;
@property (nonatomic, strong) SFSpeechAudioBufferRecognitionRequest *speechRequest;
@end
@implementation ViewController
- (void)startRecognizer
{
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
if (status == SFSpeechRecognizerAuthorizationStatusAuthorized){
NSLocale *local =[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
SFSpeechRecognizer *sf =[[SFSpeechRecognizer alloc] initWithLocale:local];
self.speechRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
[sf recognitionTaskWithRequest:self.speechRequest delegate:self];
// should call startCapture method in main queue or it may crash
dispatch_async(dispatch_get_main_queue(), ^{
[self startCapture];
});
}
}];
}
- (void)endRecognizer
{
// END capture and END voice Reco
// or Apple will terminate this task after 30000ms.
[self endCapture];
[self.speechRequest endAudio];
}
- (void)startCapture
{
NSError *error;
self.capture = [[AVCaptureSession alloc] init];
AVCaptureDevice *audioDev = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
if (audioDev == nil){
NSLog(@"Couldn't create audio capture device");
return ;
}
// create mic device
AVCaptureDeviceInput *audioIn = [AVCaptureDeviceInput deviceInputWithDevice:audioDev error:&error];
if (error != nil){
NSLog(@"Couldn't create audio input");
return ;
}
// add mic device in capture object
if ([self.capture canAddInput:audioIn] == NO){
NSLog(@"Couldn't add audio input");
return ;
}
[self.capture addInput:audioIn];
// export audio data
AVCaptureAudioDataOutput *audioOutput = [[AVCaptureAudioDataOutput alloc] init];
[audioOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
if ([self.capture canAddOutput:audioOutput] == NO){
NSLog(@"Couldn't add audio output");
return ;
}
[self.capture addOutput:audioOutput];
[audioOutput connectionWithMediaType:AVMediaTypeAudio];
[self.capture startRunning];
}
-(void)endCapture
{
if (self.capture != nil && [self.capture isRunning]){
[self.capture stopRunning];
}
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
[self.speechRequest appendAudioSampleBuffer:sampleBuffer];
}
// some Recognition Delegate
@end