In the flutter, I am trying to use Microsoft continuous speech to text transcription for my app. In the android java code, I tried the api calling, I got the recognizing and recognized text in the console. But on passing that text to the client side I am not getting any success. Please help me on this, I am sharing the code of my app as follows.
Main Java file:
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.EventChannel;
import com.microsoft.cognitiveservices.speech.*;
import com.microsoft.cognitiveservices.speech.audio.AudioConfig;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.microsoft.cognitiveservices.speech.ResultReason;
import com.microsoft.cognitiveservices.speech.SpeechConfig;
import com.microsoft.cognitiveservices.speech.SpeechRecognitionResult;
import com.microsoft.cognitiveservices.speech.SpeechRecognizer;
import java.util.concurrent.Future;
import static android.Manifest.permission.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import android.os.Handler;
public class MainActivity extends FlutterActivity {
public static final String STREAM = "speechtotext/android";
private static String YourSubscriptionKey = "****";
private static String YourServiceRegion = "japaneast";
private EventChannel.EventSink attachEvent;
private Handler handler;
private int count = 1;
String result;
private final Runnable runnable = new Runnable() {
@Override
public void run() {
SpeechConfig speechConfig = SpeechConfig.fromSubscription(YourSubscriptionKey,
YourServiceRegion);
speechConfig.setSpeechRecognitionLanguage("ja-JP");
AudioConfig audioConfig = AudioConfig.fromDefaultMicrophoneInput();
SpeechRecognizer speechRecognizer = new SpeechRecognizer(speechConfig,
audioConfig);
int requestCode = 5; // unique code for the permission request
ActivityCompat.requestPermissions(MainActivity.this,
new String[] { RECORD_AUDIO, INTERNET }, requestCode);
speechRecognizer.recognized.addEventListener((s, e) -> {
if (e.getResult().getReason() == ResultReason.RecognizedSpeech) {
System.out.println("RECOGNIZED: Text=" + e.getResult().getText());
attachEvent.success("Returned value is " + e.getResult().getText());
} else if (e.getResult().getReason() == ResultReason.NoMatch) {
System.out.println("NOMATCH: Speech could not be recognized.");
try {
speechRecognizer.stopContinuousRecognitionAsync().get();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
});
speechRecognizer.canceled.addEventListener((s, e) -> {
System.out.println("CANCELED: Reason=" + e.getReason());
if (e.getReason() == CancellationReason.Error) {
System.out.println("CANCELED: ErrorCode=" + e.getErrorCode());
System.out.println("CANCELED: ErrorDetails=" + e.getErrorDetails());
System.out.println(
"CANCELED: Did you set the speech resource key and region values?");
}
try {
speechRecognizer.stopContinuousRecognitionAsync().get();
attachEvent.endOfStream();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
});
speechRecognizer.sessionStopped.addEventListener((s, e) -> {
System.out.println("\n top Session stopped event.");
try {
speechRecognizer.stopContinuousRecognitionAsync().get();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
});
try {
speechRecognizer.startContinuousRecognitionAsync().get();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new EventChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), STREAM).setStreamHandler(
new EventChannel.StreamHandler() {
@Override
public void onListen(Object args, final EventChannel.EventSink events) {
System.out.println("Listening...");
attachEvent = events;
count = 1;
handler = new Handler();
events.success("HELLO");
runnable.run();
}
@Override
public void onCancel(Object args) {
System.out.println("Cancelled...");
handler.removeCallbacks(runnable);
handler = null;
count = 1;
attachEvent = null;
System.out.println("StreamHandler - onCancelled: ");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
handler = null;
attachEvent = null;
}
}
Dart file:
import 'dart:async'; // StreamSubscription
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; // For eventchannel
class NewReport extends StatefulWidget {
const NewReport({super.key});
@override
State<NewReport> createState() => _NewReportState();
}
class _NewReportState extends State<NewReport> {
static const ioschannel = EventChannel('speechtotext/ios');
static const androidchannel = EventChannel('speechtotext/android');
late StreamSubscription speechToTextSubscription;
final TextEditingController _controller = TextEditingController();
_startReading() {
speechToTextSubscription =
androidchannel.receiveBroadcastStream().listen((event) {
setState(() {
print("*********************************************");
print(event);
_controller.text = event;
_controller.selection = TextSelection.fromPosition(
TextPosition(offset: _controller.text.length));
});
});
}
_saveReport() {
setState(() {
_controller.text = _controller.text;
});
speechToTextSubscription.cancel();
FirebaseFirestore.instance.collection("Diary").add({
"description": _controller.text,
"title": "Diary" + DateTime.now().toString(),
"createdDate": DateTime.now()
});
_controller.clear();
}
@override
Widget build(BuildContext context) {
final screenHeight = MediaQuery.of(context).size.height;
final screenWidth = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(
title: const Text('New Report'),
),
body: SingleChildScrollView(
child: Column(
children: [
const SizedBox(
height: 10,
),
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 0.5)),
height: screenHeight * 0.75,
width: screenWidth * 0.95,
padding: const EdgeInsets.all(10),
child: SingleChildScrollView(
child: TextField(
decoration: const InputDecoration.collapsed(hintText: null),
controller: _controller,
minLines: 20,
maxLines: null,
),
),
),
const SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
),
),
onPressed: () => _startReading(),
child: const Icon(Icons.mic),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
// shape: const CircleBorder(),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
),
),
onPressed: () => _saveReport(),
child: const Icon(Icons.save_as_rounded),
),
],
),
],
),
),
);
}
}