不带按钮的颤动语音命令

时间:2020-03-24 04:32:05

标签: flutter

我希望能够通过语音控制我的应用。 问题是,我今天在pub.dev和论坛中发现的solutiosn以这种方式工作

  1. 按下按钮
  2. 麦克风已激活,您有几秒钟的时间说出所需的命令
  3. 指定文字
  4. 命令已执行

我需要的是这样

  1. 启动应用
  2. 它一直在监听并等待非常特定的命令,例如关闭,暂停,恢复
  3. 当它检测到非常具体的一个单词命令时,便会执行该操作。

基本上,我想要诸如“ Hi siri”,“ ok google”之类的交互功能,而不是想要“ pause”

谁能推荐解决方案

1 个答案:

答案 0 :(得分:0)

我的建议是您使用平台特定的代码。我使用Java与Flutter应用程序进行交互。一旦做到这一点,就可以在检测到任何动作词时指定某些动作。

class Dashboard extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _DashboardState();
}

class _DashboardState extends State<Dashboard> {
  SpeechRecognition _speechRecognition;
  bool _isAvailable = false;
  bool _isListening = false;

  String resultText = "";

  static const platform = const MethodChannel("com.example.javaConnect/javaConnect");


  void test() async {
    String value; 

    try{
      value = await platform.invokeMethod("startService"); // startService is the method used in the Java code
    }catch (e){
      print(e);
    }

    print(value);
  }

  @override
  void initState() {
    super.initState();
    initSpeechRecognizer();
    if (_isAvailable && !_isListening) {
      _speechRecognition.listen(locale: "en_US")
      .then(
        (result) => print('$result'),
      );
    }
  }

  void initSpeechRecognizer() {
    _speechRecognition = SpeechRecognition();

    _speechRecognition.setAvailabilityHandler(
        (bool result) => setState(() => _isAvailable = result));

    _speechRecognition.setRecognitionStartedHandler(
        () => setState(() => _isListening = true));

    _speechRecognition.setRecognitionResultHandler(
      (String speech) {
        setState(() => resultText = speech);
        if ( speech == "no no no" ){
            test();
        }  
      }
    );

    _speechRecognition.setRecognitionCompleteHandler(
        () {
          setState(() => _isListening = false);
        });

    _speechRecognition.activate().then(
          (result) => setState(() => _isAvailable = result),
        );

    final String number = "123456789";
  }

  void filterVoicings(String result) {
    print(result);
  }

  void recallMethod(){
    Timer.periodic(new Duration(seconds: 10), (timer) {
      if (_isAvailable && !_isListening) {
        _speechRecognition.listen(locale: "en_US")
            .then(
              (result) => print('$result'),
        ).catchError((error) => print('$error'));
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        child: Column(
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(10),
              child: Text(resultText),
            ),
            Row(
              children: <Widget>[
                MaterialButton(
                  onPressed: () {
                    recallMethod();
                  },
                  child: Text("Listen"),
                  minWidth: MediaQuery.of(context).size.width * 0.5,
                  height: MediaQuery.of(context).size.height * 0.25,
                ),
                MaterialButton(
                  onPressed: () {
                    if (_isListening) {
                      _speechRecognition.stop()
                      .then(
                            (result) => setState(() => _isListening = result)
                          );
                    }
                  },
                  child: Text("Stop"),
                  minWidth: MediaQuery.of(context).size.width * 0.5,
                  height: MediaQuery.of(context).size.height * 0.25,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

这里是视频的链接,可以正确地解释如何使用带有本机代码的flutter。 Flutter with native code (Android Java)