flutter_tts icon indicating copy to clipboard operation
flutter_tts copied to clipboard

`awaitSpeakCompletion(true)` crashes application

Open nt4f04uNd opened this issue 3 years ago • 4 comments

🐛 Bug Report

Expected behavior

no crash

Reproduction steps

speak and awaitSpeakCompletion(true)

code sample
import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(App());
}

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  final tts = FlutterTts();

  Future<void> speak(String text) {
    tts.speak(text);
    return tts.awaitSpeakCompletion(true);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App',
      home: Scaffold(
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              speak('1');
            },
            child: Text('speak'),
          ),
        ),
      ),
    );
  }
}

Configuration

???

logs
I/e.flutter_issu( 7547): ProcessProfilingInfo new_methods=574 is saved saved_to_disk=1 resolve_classes_delay=8000
D/TTS     ( 7547): Utterance ID has started: 0b3edc1d-fbc7-45e9-aa80-ed69ecbe02c0
D/TTS     ( 7547): Utterance ID has completed: 0b3edc1d-fbc7-45e9-aa80-ed69ecbe02c0
D/AndroidRuntime( 7547): Shutting down VM
E/AndroidRuntime( 7547): FATAL EXCEPTION: main
E/AndroidRuntime( 7547): Process: com.example.flutter_issue, PID: 7547
E/AndroidRuntime( 7547): java.lang.NullPointerException: Attempt to invoke interface method 'void io.flutter.plugin.common.MethodChannel$Result.success(java.lang.Object)' on a null object reference
E/AndroidRuntime( 7547): 	at com.tundralabs.fluttertts.FlutterTtsPlugin$2.run(FlutterTtsPlugin.java:177)
E/AndroidRuntime( 7547): 	at android.os.Handler.handleCallback(Handler.java:873)
E/AndroidRuntime( 7547): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 7547): 	at android.os.Looper.loop(Looper.java:201)
E/AndroidRuntime( 7547): 	at android.app.ActivityThread.main(ActivityThread.java:6810)
E/AndroidRuntime( 7547): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 7547): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
E/AndroidRuntime( 7547): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
I/Process ( 7547): Sending signal. PID: 7547 SIG: 9
Lost connection to device.
Exited (sigterm)

flutter doctor -v
[✓] Flutter (Channel master, 2.1.0-13.0.pre.386, on Microsoft Windows [Version 10.0.19041.867], locale ru-RU)
    • Flutter version 2.1.0-13.0.pre.386 at c:\dev\src\flutter
    • Framework revision 66af44c9fe (54 minutes ago), 2021-04-01 10:22:37 -0700
    • Engine revision 9651d11fe8
    • Dart version 2.13.0 (build 2.13.0-190.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at C:\Users\danya\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.7)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.7.30621.155
    • Windows 10 SDK version 10.0.19041.0

[✓] Android Studio (version 4.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 50.0.1
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[✓] IntelliJ IDEA Community Edition (version 2020.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.3
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.55.0)
    • VS Code at C:\Users\danya\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.21.0

[✓] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19041.867]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 89.0.4389.90
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 89.0.774.63

• No issues found!

Version: 3.0.0

Platform:

  • [?] :iphone: iOS - don't know
  • [x] :robot: Android

nt4f04uNd avatar Apr 07 '21 13:04 nt4f04uNd

i would suggest removing native implementation of this API, becuase a better choice would be to allow registering multiple listeners (of all kinds) and have one internal platform listener that notifies them all and implement awaitSpeakCompletion on dart side via regular Completer

nt4f04uNd avatar Apr 07 '21 13:04 nt4f04uNd

try using the await keyword. Also I'll look into moving it into the dart code rather than the native code as you mentioned.

dlutton avatar Apr 07 '21 17:04 dlutton

ok by the way with await it still crashes, i will just wait for a fix, this is not urgent to me

nt4f04uNd avatar Apr 07 '21 19:04 nt4f04uNd

For those looking for a workaround for now. Here's an equivalent to awaitSpeakCompletion(true) implemented with Dart Completer:

play() async {
      final completer = Completer<void>();
      await flutterTts.speak(text);
      flutterTts.setCompletionHandler(() {
        completer.complete();
      });
      return completer.future;
}

AlienKevin avatar May 31 '22 22:05 AlienKevin