csdcorp / speech_to_text

A Flutter plugin that exposes device specific text to speech recognition capability.
BSD 3-Clause "New" or "Revised" License
351 stars 218 forks source link

Flutter SpeechToText doesn't work on Android #353

Closed DevDJpl closed 1 year ago

DevDJpl commented 1 year ago

I have all the permissions added, it doesn't show me any errors when compiling in Android Studio, but it doesn't work completely SpeechToText both in the mobile emulator and on the physical device only works on the web emulator.

Permissions: <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>

`

`

Logs: I/flutter ( 5540): listening D/BluetoothHeadset( 5540): Proxy object connected D/EGL_emulation( 5540): app_time_stats: avg=8.60ms min=1.51ms max=29.80ms count=59 D/EGL_emulation( 5540): app_time_stats: avg=2.44ms min=1.60ms max=5.03ms count=60 D/EGL_emulation( 5540): app_time_stats: avg=15.34ms min=1.54ms max=100.35ms count=55 D/EGL_emulation( 5540): app_time_stats: avg=5.19ms min=1.44ms max=26.38ms count=60 D/EGL_emulation( 5540): app_time_stats: avg=13.63ms min=1.47ms max=101.13ms count=55 I/flutter ( 5540): notListening I/flutter ( 5540): done I/flutter ( 5540): SpeechRecognitionError msg: error_speech_timeout, permanent: true

Pubspec: version: 1.0.0+1

environment: sdk: '>=2.18.2 <3.0.0'

dependencies: flutter: sdk: flutter

cupertino_icons: ^1.0.2 font_awesome_flutter: ^10.2.1 percent_indicator: ^4.0.1 google_fonts: ^3.0.1 avatar_glow: ^2.0.2 provider: ^5.0.0 #^6.0.4 flutter_launcher_icons: ^0.10.0 package_info_plus: ^3.0.2 device_info_plus: ^8.0.0 url_launcher: ^6.1.6 text_to_speech: ^0.2.3 speech_to_text: ^6.1.1 google_cloud_translation: ^0.0.3

SpeechPage code: import 'package:flutter/material.dart'; import 'package:avatar_glow/avatar_glow.dart'; import '../providers/theme_provider.dart'; import '../widgets/substring_highlighted.dart'; import '../api/speach_api.dart'; import '../utils.dart';

class TalkToAi extends StatefulWidget { const TalkToAi({Key? key}) : super(key: key);

@override State<TalkToAi> createState() => _TalkToAiState(); }

class _TalkToAiState extends State<TalkToAi> { @override

String text = "Press the button and start speaking"; bool isListening = false;

Widget build(BuildContext context){ return Scaffold( body: SingleChildScrollView( reverse: true, padding: const EdgeInsets.all(30).copyWith(bottom: 150), child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ SubstringHighlight( text: text, terms: Command.all, textStyle: TextStyle( fontSize: 32.0, color: Colors.black, fontWeight: FontWeight.w400, ), textStyleHighlight: TextStyle( fontSize: 32.0, color: Colors.red, fontWeight: FontWeight.w400, ), ), AvatarGlow( glowColor: yellowColor, endRadius: 80, animate: isListening, showTwoGlows: true, repeatPauseDuration: Duration(milliseconds: 100), child: CircleAvatar( radius: 50, backgroundColor: yellowColor, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ IconButton( icon: Icon(Icons.mic, size: 32, color: Color(0xffffffff)), onPressed: toggleRecording, ), ], ), ), //onPressed: toggleRecording, ), ], ), ), ); }

Future toggleRecording() => SpeechApi.toggleRecording( onResult: (text) => setState(() => this.text = text), onListening: (isListening){ setState(() => this.isListening = isListening); if(!isListening){ Future.delayed(Duration(seconds: 1), (){ print(text); Utils.scanText(text); }); } }, ); }

SpeechApi code: import 'package:flutter/cupertino.dart'; import 'package:speech_to_text/speech_to_text.dart';

class SpeechApi { static final _speech = SpeechToText();

static Future<bool> toggleRecording({ required Function(String text) onResult, required ValueChanged<bool> onListening, }) async { if(_speech.isListening){ _speech.stop(); return true; }

final isAvailable = await _speech.initialize( debugLogging: true, onStatus: (status) => onListening(_speech.isListening), onError: (e) => print('Error: $e'), );

if(isAvailable){ _speech.listen(onResult: (value) => onResult(value.recognizedWords)); }

return isAvailable; } }

Utils code: import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart';

class Command { static final all = [browser1, browser2];

static const browser1 = 'open'; static const browser2 = 'go to'; `}

class Utils { static void scanText(String rawText){ final text = rawText.toLowerCase();

if(text.contains(Command.browser1)){ final url = _getTextAfterCommand(text: text, command: Command.browser1);

openLink(url: url); }else if(text.contains(Command.browser2)){ final url = _getTextAfterCommand(text: text, command: Command.browser2);

openLink(url: url); } }

static String _getTextAfterCommand({ required String text, required String command, }){ final indexCommand = text.indexOf(command); final indexAfter = indexCommand + command.length;

if(indexCommand == -1){ return ""; }else{ return text.substring(indexAfter).trim(); } }

static Future openLink({ required String url, }) async { if(url.trim().isEmpty){ _launchInBrowser(Uri.parse("https://google.com")); }else{ _launchInBrowser(Uri.parse("https://$url")); } }

static Future<void> _launchInBrowser(Uri url) async { if(!await launchUrl(url, mode: LaunchMode.externalApplication)){ throw 'Could not launch $url'; } }

static Future<void> _launchInWebViewOrVC(Uri url) async { if(!await launchUrl(url, mode: LaunchMode.inAppWebView)){ throw 'Could not launch $url'; } }

}

And I'm a beginner with Android and I have no idea what's wrong and I've been looking for a solution for several days. I need this to work because I'm writing my own voice assistant

sowens-csd commented 1 year ago

Is the example app working on the physical device? Have you checked the README notes for ensuring the voice assistant is properly installed on the Android device? What is the model and version of the Android device?

sowens-csd commented 1 year ago

Any updates on this?

DevDJpl commented 1 year ago

I checked on a virtual device and on the phone. It doesn't work on both virtual and phone. The Google Assistant works normally on the phone and SpeachToText did not work on the device. I checked it on the browser where everything worked normally.

DevDJpl commented 1 year ago

On any device doesn't work. We use Android version on app 8+

sowens-csd commented 1 year ago

Have you tried the example app on the same device?

DevDJpl commented 1 year ago

Yes I tried

dnfatec commented 1 year ago

I have same problem in Android 8.1, but in recentily version, no problems!

sowens-csd commented 1 year ago

It could be a problem with an older version of Android. I only have test devices for 10, 11 and 13, all of which work. The minSDK is set to 21 but it's possible I introduced an incompatibility with older versions. Can you confirm what version of Android you are using and whether it works on newer versions?

dnfatec commented 1 year ago

Hello!

I did new tests, with old versions (Android 8 and Android 8.1) in other smartphones and no have problems. Today, i did new configurations, this smartphone

[image: image.png]

Google permissions to microphone no did active. But after active, the capture voice in the app normalized

Thanks

Danilo R. Gomes

Em qua., 1 de fev. de 2023 às 15:28, Stephen Owens @.***> escreveu:

It could be a problem with an older version of Android. I only have test devices for 10, 11 and 13, all of which work. The minSDK is set to 21 but it's possible I introduced an incompatibility with older versions. Can you confirm what version of Android you are using and whether it works on newer versions?

— Reply to this email directly, view it on GitHub https://github.com/csdcorp/speech_to_text/issues/353#issuecomment-1412527967, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJJTB4I3WXEP5ULW6WYKFG3WVKTORANCNFSM6AAAAAASSJ7A5E . You are receiving this because you commented.Message ID: @.***>

DevDJpl commented 1 year ago

I use Android 11 device and doesn't works

dnfatec commented 1 year ago

No problems in Android 11 (testing in physics smartphone "Motorolas")

Danilo R. Gomes Professor Fatec Itapetininga Curso Análises e Desenvolvimento de Sistemas

Em qui., 2 de fev. de 2023 às 14:04, DevDJ @.***> escreveu:

I use Android 11 device and doesn't works

— Reply to this email directly, view it on GitHub https://github.com/csdcorp/speech_to_text/issues/353#issuecomment-1414074718, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJJTB4I2ZROQ7NU7J6XOE2DWVPSK7ANCNFSM6AAAAAASSJ7A5E . You are receiving this because you commented.Message ID: @.***>

DevDJpl commented 1 year ago

I tested on Sony Xperia and not works

sowens-csd commented 1 year ago

If you're still having problems can you post a log from a phone that isn't working. Make sure to use the debugLogging: true setting in the initialize method before you do the capture.

DevDJpl commented 1 year ago

And how to do it? Because I only know how to do it in Android Studio

nyck33 commented 10 months ago

The app in speech-to-text/speech-to-text/example works. That's a nested folder with the same name.