Reputation: 11
I am newbie to flutter and java.
I am trying to send a stream of data from speechRecognizer.recognized.addeventlistener function from java to flutter. speechRecognizer.recognized.addeventlistener is using Microsoft Azure speech to text API .But the sink.success, attachEvent.success in my case is not sending any string to flutter.
I have attached a main.dart, homepage.dart code as well as java code here. Hope you guys will help me to find a solution.
For safety reasons, I have removed YourSubscriptionKey and YourServiceRegion.
main.dart
import 'package:flutter/material.dart';
import 'home_page.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Event Channel Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Demo page'),
);
}
}
home_page.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
static const stream = EventChannel(
'com.chamelalaboratory.demo.flutter_event_channel/eventChannel');
late StreamSubscription _streamSubscription;
String _currentValue = "";
void _startListener() {
_streamSubscription = stream.receiveBroadcastStream().listen(_listenStream);
}
void _cancelListener() {
_streamSubscription.cancel();
setState(() {
_currentValue = "Start Recording...";
});
}
void _listenStream(value) {
debugPrint("Received From Native: $value\n");
setState(() {
_currentValue = value;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(_currentValue.toUpperCase(), textAlign: TextAlign.justify),
const SizedBox(
height: 50,
),
//Start Btn
ElevatedButton(
onPressed: () => _startListener(),
child: Text("Record".toUpperCase()),
),
const SizedBox(
height: 50,
),
//Cancel Btn
ElevatedButton(
onPressed: () => _cancelListener(),
child: Text("Stop".toUpperCase()),
),
],
),
),
);
}
}
MainActivity.java
package com.example.sounddiaryapp;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import com.microsoft.cognitiveservices.speech.*;
import com.microsoft.cognitiveservices.speech.audio.*;
import java.util.concurrent.ExecutionException;
import java.io.IOException;
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 java.util.*;
import java.util.concurrent.Future;
import static android.Manifest.permission.*;
import java.util.concurrent.TimeUnit;
import android.os.Handler;
import java.util.Objects;
import io.flutter.plugin.common.EventChannel;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "codecommon";
private static String YourSubscriptionKey = "";
private static String YourServiceRegion = "";
public static final String STREAM = "com.chamelalaboratory.demo.flutter_event_channel/eventChannel";
final String TAG_NAME = "From_Native";
private EventChannel.EventSink attachEvent;
private int count = 1;
public String receivedText = "";
List<String> textList=new ArrayList<String>();
private Handler handler;
SpeechConfig speechConfig = SpeechConfig.fromSubscription(YourSubscriptionKey,
YourServiceRegion);
AudioConfig audioConfig = AudioConfig.fromDefaultMicrophoneInput();
SpeechRecognizer speechRecognizer = new SpeechRecognizer(speechConfig, audioConfig);
private final Runnable runnable = new Runnable() {
@Override
public void run() {
try{
int requestCode = 5; // unique code for the permission request
ActivityCompat.requestPermissions(MainActivity.this,
new String[] { RECORD_AUDIO, INTERNET }, requestCode);
try {
speechRecognizer.recognized.addEventListener((s, e) -> {
if (e.getResult().getReason() == ResultReason.RecognizedSpeech) {
System.out.println("RECOGNIZED: Text=" + e.getResult().getText());
String receivedText = e.getResult().getText();
attachEvent.success(receivedText);
} else if (e.getResult().getReason() == ResultReason.NoMatch) {
System.out.println("NOMATCH: Speech could not be recognized.");
try {
speechRecognizer.stopContinuousRecognitionAsync().get();
// attachEvent.endOfStream();
}
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();
// attachEvent.endOfStream();
}
catch (Exception ex ) {
System.out.println(ex.getMessage());
}
});
speechRecognizer.startContinuousRecognitionAsync().get();
} catch(Exception e) {
System.out.println("*****************************************************");
System.out.println(e.getMessage());
try {
speechRecognizer.stopContinuousRecognitionAsync().get();
// attachEvent.endOfStream();
}
catch (Exception ex ) {
System.out.println(ex.getMessage());
}
}
} catch (Exception ex) {
System.out.println("+++++++++++++++++++++++++++++ Unexpected exception: " + ex.getMessage());
// recognizer.stopContinuousRecognitionAsync().get();
assert (false);
System.exit(1);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new EventChannel(Objects.requireNonNull(getFlutterEngine()).getDartExecutor(), STREAM).setStreamHandler(
new EventChannel.StreamHandler() {
@Override
public void onListen(Object args, final EventChannel.EventSink events) {
Log.w(TAG_NAME, "Adding listener");
attachEvent = events;
count = 1;
handler = new Handler();
runnable.run();
}
@Override
public void onCancel(Object args) {
Log.w(TAG_NAME, "Cancelling listener");
try{
stopSpeechToText();
}
catch (Exception ex) {
System.out.println("-------------------------------------");
System.out.println(ex);
}
handler.removeCallbacks(runnable);
handler = null;
count = 1;
attachEvent = null;
System.out.println("StreamHandler - onCancelled: ");
}
}
);
}
public void stopSpeechToText() throws InterruptedException, ExecutionException,IOException {
speechRecognizer .sessionStopped.addEventListener((s, e) -> {
System.out.println("\n top Session stopped event.");
try {
speechRecognizer .stopContinuousRecognitionAsync().get();
// attachEvent.endOfStream();
}
catch (Exception ex ) {
System.out.println(ex.getMessage());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
handler = null;
attachEvent = null;
}
}
Upvotes: 1
Views: 519