flutter_tts icon indicating copy to clipboard operation
flutter_tts copied to clipboard

speak() not work first time in android.

Open troytang opened this issue 4 years ago โ€ข 2 comments

๐Ÿ› Bug Report

Detail

flutterTts.speak() not work when the first time invoke, but it is working well after the second time

Configuration

flutter doctor

[โœ“] Flutter (Channel stable, 2.0.4, on macOS 11.2.1 20D74 darwin-x64, locale zh-Hans-CN) [โœ“] Android toolchain - develop for Android devices (Android SDK version 30.0.2) [!] Xcode - develop for iOS and macOS ! CocoaPods 1.9.1 out of date (1.10.0 is recommended). CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side. Without CocoaPods, plugins will not work on iOS or macOS. For more info, see https://flutter.dev/platform-plugins To upgrade see https://guides.cocoapods.org/using/getting-started.html#installation for instructions. [โœ—] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome) ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable. [โœ“] Android Studio [โœ“] Android Studio [โœ“] Android Studio [โœ“] VS Code (version 1.55.0) [!] Proxy Configuration ! NO_PROXY is not set [โœ“] Connected device (1 available)

flutter_tts:^3.0.0

log when invoke speak() first time. D/ViewRootImpl@d44231bMainActivity: ViewPostIme pointer 0 D/ViewRootImpl@d44231bMainActivity: ViewPostIme pointer 1

Platform:

  • [ ] :iphone: iOS
  • [ โˆš ] :robot: Android

troytang avatar Apr 08 '21 02:04 troytang

Can you provide more code of what you're trying to do when receiving that error? I'm not able to replicate this issue. Also this could be device related or something unrelated to this plugin since it has been mentioned in the flutter github issues section.

dlutton avatar Apr 11 '21 17:04 dlutton

Can you provide more code of what you're trying to do when receiving that error? I'm not able to replicate this issue. Also this could be device related or something unrelated to this plugin since it has been mentioned in the flutter github issues section.

Thx for your reply. I will try other devices and show my code here:

import 'dart:io';

import 'package:flutter_tts/flutter_tts.dart';

mixin TtsUtil {
  static FlutterTts? _flutterTts;
  static Future<FlutterTts> get flutterTts async {
    if (_flutterTts == null) {
      _flutterTts = FlutterTts();

      // ็œ‹็œ‹่ฎพๅค‡ๆ”ฏๆŒไป€ไนˆๅฃฐ้Ÿณ๏ผŒๅŽ็ปญๅฏ้€‰
      var voices = await _flutterTts!.getVoices;

      await _flutterTts!.setLanguage('ja-JP');
      await _flutterTts!.setSpeechRate(0.5);
      await _flutterTts!.setVolume(1.0);
      await _flutterTts!.setPitch(1.0);

      _flutterTts!.setStartHandler(() {
        print('Tts Playing');
        _ttsState = TtsState.playing;
      });
      _flutterTts!.setCompletionHandler(() {
        print('Tts Complete');
        _ttsState = TtsState.stopped;
      });
      _flutterTts!.setProgressHandler(
          (String text, int startOffset, int endOffset, String word) {
        print(
            'text = $text, startOffset = $startOffset, endOffset = $endOffset, word = $word');
      });
      _flutterTts!.setErrorHandler((msg) {
        print('error: $msg');
        _ttsState = TtsState.stopped;
      });
      _flutterTts!.setCancelHandler(() {
        print('Tts Cancel');
        _ttsState = TtsState.stopped;
      });

      if (Platform.isIOS) {
        await _flutterTts!.setSharedInstance(true);
        await _flutterTts!
            .setIosAudioCategory(IosTextToSpeechAudioCategory.playAndRecord, [
          IosTextToSpeechAudioCategoryOptions.allowBluetooth,
          IosTextToSpeechAudioCategoryOptions.allowBluetoothA2DP,
          IosTextToSpeechAudioCategoryOptions.mixWithOthers,
          IosTextToSpeechAudioCategoryOptions.defaultToSpeaker
        ]);
        _flutterTts!.setPauseHandler(() {
          print('Tts Paused');
          _ttsState = TtsState.paused;
        });
        _flutterTts!.setContinueHandler(() {
          print('Tts Continued');
          _ttsState = TtsState.continued;
        });
      } else if (Platform.isAndroid) {
        await _flutterTts!.setSilence(2);
        await _flutterTts!.setQueueMode(1);
      }
    }
    return _flutterTts!;
  }

  static TtsState _ttsState = TtsState.stopped;

  static bool get _isPlaying => _ttsState == TtsState.playing;
  static bool get _isStopped => _ttsState == TtsState.stopped;
  static bool get _isPaused => _ttsState == TtsState.paused;
  static bool get _isContinued => _ttsState == TtsState.continued;

  static Future speak(String text) async {
    return (await flutterTts).speak(text);
  }
}

enum TtsState { playing, stopped, paused, continued }

troytang avatar Apr 13 '21 11:04 troytang