Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter SpeechToText doesn't work on Android #353

Closed
DevDJpl opened this issue Dec 2, 2022 · 15 comments
Closed

Flutter SpeechToText doesn't work on Android #353

DevDJpl opened this issue Dec 2, 2022 · 15 comments

Comments

@DevDJpl
Copy link

DevDJpl commented Dec 2, 2022

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"/>

<queries> <intent> <action android:name="android.speech.RecognitionService" /> </intent> </queries>

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
Copy link
Contributor

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
Copy link
Contributor

Any updates on this?

@DevDJpl
Copy link
Author

DevDJpl commented Jan 9, 2023

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
Copy link
Author

DevDJpl commented Jan 10, 2023

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

@sowens-csd
Copy link
Contributor

Have you tried the example app on the same device?

@DevDJpl
Copy link
Author

DevDJpl commented Jan 24, 2023

Yes I tried

@dnfatec
Copy link

dnfatec commented Jan 24, 2023

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

@sowens-csd
Copy link
Contributor

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
Copy link

dnfatec commented Feb 2, 2023 via email

@DevDJpl
Copy link
Author

DevDJpl commented Feb 2, 2023

I use Android 11 device and doesn't works

@dnfatec
Copy link

dnfatec commented Feb 3, 2023 via email

@DevDJpl
Copy link
Author

DevDJpl commented Feb 5, 2023

I tested on Sony Xperia and not works

@sowens-csd
Copy link
Contributor

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
Copy link
Author

DevDJpl commented Feb 5, 2023

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

@nyck33
Copy link

nyck33 commented Jul 23, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants