Skip to content

Instantly share code, notes, and snippets.

View dev-aritra's full-sized avatar
🤖
Working

Aritra Das dev-aritra

🤖
Working
View GitHub Profile
public interface ISpeechToText
{
void StartSpeechToText();
void StopSpeechToText();
}
using AVFoundation;
using Foundation;
using Speech;
using System;
using Xamarin.Forms;
using XFSpeechDemo.iOS;
[assembly: Xamarin.Forms.Dependency(typeof(SpeechToTextImplementation))]
namespace XFSpeechDemo.iOS
{
private void AskForSpeechPermission()
{
SFSpeechRecognizer.RequestAuthorization((SFSpeechRecognizerAuthorizationStatus status) =>
{
switch (status)
{
case SFSpeechRecognizerAuthorizationStatus.Authorized:
MessagingCenter.Send<ISpeechToText>(this, "Authorized");
break;
case SFSpeechRecognizerAuthorizationStatus.Denied:
private AVAudioEngine _audioEngine = new AVAudioEngine();
private SFSpeechRecognizer _speechRecognizer = new SFSpeechRecognizer();
private SFSpeechAudioBufferRecognitionRequest _recognitionRequest;
private SFSpeechRecognitionTask _recognitionTask;
private string _recognizedString;
private NSTimer _timer;
private bool isNotContinious;
public void StartSpeechToText()
{
if (_audioEngine.Running)
{
StopRecordingAndRecognition();
}
StartRecordingAndRecognizing();
}
private void StartRecordingAndRecognizing()
{
if(isNotContinious)
{
_timer = NSTimer.CreateRepeatingScheduledTimer(5, delegate
{
DidFinishTalk();
});
}
MessagingCenter.Subscribe<ISpeechToText, string>(this, "STT", (sender, args) => {
Console.WriteLine(args);
});
MessagingCenter.Subscribe<ISpeechToText>(this, "Final", (sender) => {
Console.WriteLine("Recognition finished");
});
string rec = global::Android.Content.PM.PackageManager.FeatureMicrophone;
if (rec == "android.hardware.microphone")
{
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
private void StartRecordingAndRecognizing()
{
string rec = global::Android.Content.PM.PackageManager.FeatureMicrophone;
if (rec == "android.hardware.microphone")
{
var voiceIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
voiceIntent.PutExtra(RecognizerIntent.ExtraPrompt, "Speak now");