Created
August 25, 2018 14:33
-
-
Save dev-aritra/bc702be54d81486df61a0fe2cace7efa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void StartRecordingAndRecognizing() | |
{ | |
if(isNotContinious) | |
{ | |
_timer = NSTimer.CreateRepeatingScheduledTimer(5, delegate | |
{ | |
DidFinishTalk(); | |
}); | |
} | |
_recognitionTask?.Cancel(); | |
_recognitionTask = null; | |
var audioSession = AVAudioSession.SharedInstance(); | |
NSError nsError; | |
nsError = audioSession.SetCategory(AVAudioSessionCategory.Record); | |
audioSession.SetMode(AVAudioSession.ModeMeasurement, out nsError); | |
nsError = audioSession.SetActive(true, AVAudioSessionSetActiveOptions.NotifyOthersOnDeactivation); | |
_recognitionRequest = new SFSpeechAudioBufferRecognitionRequest(); | |
var inputNode = _audioEngine.InputNode; | |
if (inputNode == null) | |
{ | |
throw new Exception(); | |
} | |
var recordingFormat = inputNode.GetBusOutputFormat(0); | |
inputNode.InstallTapOnBus(0, 1024, recordingFormat, (buffer, when) => | |
{ | |
_recognitionRequest?.Append(buffer); | |
}); | |
_audioEngine.Prepare(); | |
_audioEngine.StartAndReturnError(out nsError); | |
_recognitionTask = _speechRecognizer.GetRecognitionTask(_recognitionRequest, (result, error) => | |
{ | |
var isFinal = false; | |
if (result != null) | |
{ | |
_recognizedString = result.BestTranscription.FormattedString; | |
MessagingCenter.Send<ISpeechToText, string>(this, "STT", _recognizedString); | |
Console.WriteLine("result"); | |
if(isNotContinious) | |
{ | |
_timer.Invalidate(); | |
_timer = null; | |
_timer = NSTimer.CreateRepeatingScheduledTimer(3, delegate | |
{ | |
DidFinishTalk(); | |
}); | |
} | |
} | |
if (error != null || isFinal) | |
{ | |
MessagingCenter.Send<ISpeechToText>(this, "Final"); | |
StopRecordingAndRecognition(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment