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

Does not work in web release mode. #198

Closed sinano1107 closed 3 years ago

sinano1107 commented 3 years ago

Hi Thanks for the great package. version is using 4.1.0-nullsafety

It works fine with flutter run -d chrome, but this package does not seem to work with flutter run -release -d chrome.

Please let me know how to solve this problem. Please.

main.dart

import 'package:flutter/material.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;
import 'package:avatar_glow/avatar_glow.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter voice',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.red,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: SpeechScreen(),
    );
  }
}

class SpeechScreen extends StatefulWidget {
  @override
  _SpeechScreenState createState() => _SpeechScreenState();
}

class _SpeechScreenState extends State<SpeechScreen> {
  stt.SpeechToText _speech;
  bool _isListening = false;
  String _text = 'Press the button and start speaking';
  double _confidence = 1.0;

  @override
  void initState() {
    super.initState();
    _speech = stt.SpeechToText();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Confidence: ${(_confidence * 100.0).toStringAsFixed(1)}%'),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: AvatarGlow(
        animate: _isListening,
        glowColor: Theme.of(context).primaryColor,
        endRadius: 75.0,
        duration: const Duration(milliseconds: 2000),
        repeatPauseDuration: const Duration(milliseconds: 100),
        repeat: true,
        child: FloatingActionButton(
          onPressed: _listen,
          child: Icon(_isListening ? Icons.mic : Icons.mic_none),
        ),
      ),
      body: SingleChildScrollView(
        reverse: true,
        child: Container(
          padding: const EdgeInsets.fromLTRB(30.0, 30.0, 30.0, 150.0),
          child: Text(
            _text,
            style: const TextStyle(
              fontSize: 32.0,
              color: Colors.black,
              fontWeight: FontWeight.w400,
            ),
          ),
        ),
      ),
    );
  }

  void _listen() async {
    if (!_isListening) {
      bool available = await _speech.initialize(
        onStatus: (val) => print('onStatus: $val'),
        onError: (val) => print('onError: $val'),
      );
      if (available) {
        setState(() => _isListening = true);
        _speech.listen(
            onResult: (val) => setState(() {
                  _text = val.recognizedWords;
                  if (val.hasConfidenceRating && val.confidence > 0) {
                    _confidence = val.confidence;
                  }
                }));
      }
    } else {
      setState(() => _isListening = false);
      _speech.stop();
    }
  }
}
sowens-csd commented 3 years ago

Can you give me details on the problem you're seeing? Is it something that I will definitely see if I try it or is it a subtle problem?

sinano1107 commented 3 years ago

@sowens-csd Thank you. Speech recognition will not start. In debug mode, a tab appears asking for permission to use voice recognition by chrome, but it does not appear in release mode.

スクリーンショット 2021-03-31 22 13 58
sowens-csd commented 3 years ago

Thanks for the details. I'll try it out. The web support is new so there could still be problems.

sinano1107 commented 3 years ago

@sowens-csd Did you encounter the same problem?

sowens-csd commented 3 years ago

Yes, I'm seeing the same result. It seems that there is a redefinition of the SpeechRecognition class that's shadowing mine. Looks like I'm going to have to change to use the Flutter definition. I'll post a patch when it's ready.

sowens-csd commented 3 years ago

I just pushed a change that seems to resolve it, but I haven't tested it very extensively. If you have a chance could you try the latest version in the repo?

sinano1107 commented 3 years ago

@sowens-csd Thank you

# speech_to_text: ^4.0.0-nullsafety
speech_to_text:
  git:
    url: https://github.com/csdcorp/speech_to_text.git
    ref: null-safety
    path: speech_to_text

I tested it by editing Pubspec.yaml like this, but it did not work in either debug or release mode.

Maybe my testing method is wrong?

juliopuebla commented 3 years ago

Hi I tested with your example "speech_main.dart" in web in release version, and when I push the "Initialize" button this give me this error message: "speech_not_suported - true", but when I run it in debug mode this works great. how can I solve this? Thanks in advance.

sowens-csd commented 3 years ago

Did you try it against the repo version that I posted?

juliopuebla commented 3 years ago

Did you try it against the repo version that I posted?

Yes, I did it like this:

speech_to_text: git: url: https://github.com/csdcorp/speech_to_text.git ref: null-safety path: speech_to_text

But in this way it does not work in debug mode or in release mode.

sowens-csd commented 3 years ago

Thanks for letting me know. I'll go back to that version and see what I did wrong.

sinano1107 commented 3 years ago

@sowens-csd Do you think you can solve it?

sowens-csd commented 3 years ago

I've recreated the problem but don't yet have a solution. It worked when I last checked it and now doesn't work at all. Hope to have more information soon.

sowens-csd commented 3 years ago

resolved, please try again?

juliopuebla commented 3 years ago

resolved, please try again?

Many thanks. It works great.

sowens-csd commented 3 years ago

This is now released as 4.1.1-nullsafety