Flutter plugin constructor in class cannot be applied to given types

有些话、适合烂在心里 提交于 2020-07-19 06:03:12

问题


I'm trying to build a simple speech to text application which should continuously listen. Therefore I searched pub.dev for a fitting plugin but unfortunately, there aren't any like that. So the next step would be to integrate it by myself natively. There is one plugin for android called "Droid Speech 2.0"(kotlin). I successfully integrated into a normal android kotlin project. Now I'm trying to build a flutter plugin for me and thought about using platform channels. But because I don't have much android and especially kotlin experience, I'm running into some errors.

Error:

Downloads/simple_speech_to_text/example/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java:15: error: constructor SimpleSpeechToTextPlugin in class SimpleSpeechToTextPlugin cannot be applied to given types;
    flutterEngine.getPlugins().add(new com.example.simple_speech_to_text.SimpleSpeechToTextPlugin());
                                   ^
  required: Activity
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

Here is my code:

class SimpleSpeechToTextPlugin(private var activity: Activity): EventChannel.StreamHandler{

  private var dks: Dks? = null


  companion object{
    @JvmStatic
    fun registerWith(registrar: Registrar){
      Log.d("test","companion object")
      val channel = EventChannel(registrar.messenger(), "com.examplesimple_speech_to_text/stream")
      channel.setStreamHandler(SimpleSpeechToTextPlugin(registrar.activity()))
    }
  }


  override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
    Log.d("test", "onListen")
    createListener(events)
  }

  override fun onCancel(arguments: Any?) {}


  private fun createListener(event: EventChannel.EventSink?){
    Log.d("test", "create Listener")
    if(activity != null) {
      Log.d("test","activity is not null")
      val act = activity as FragmentActivity
      dks = Dks(activity!!.application, act.supportFragmentManager, object : DksListener {
        override fun onDksLiveSpeechResult(liveSpeechResult: String) {
          Log.d(activity!!.application.packageName, "Speech result - $liveSpeechResult")
          event?.success(liveSpeechResult)
        }

        override fun onDksFinalSpeechResult(speechResult: String) {
          Log.d(activity!!.packageName, "Final speech result - $speechResult")
        }

        override fun onDksLiveSpeechFrequency(frequency: Float) {}

        override fun onDksLanguagesAvailable(defaultLanguage: String?, supportedLanguages: ArrayList<String>?) {
          Log.d(activity!!.packageName, "defaultLanguage - $defaultLanguage")
          Log.d(activity!!.packageName, "supportedLanguages - $supportedLanguages")

          if (supportedLanguages != null && supportedLanguages.contains("en-IN")) {
            // Setting the speech recognition language to english india if found
            dks?.currentSpeechLanguage = "de-DE"
          }
        }

        override fun onDksSpeechError(errMsg: String) {
          Toast.makeText(activity, errMsg, Toast.LENGTH_SHORT).show()
        }
      })
    }

    if(dks != null) {
      dks!!.startSpeechRecognition()
    }
  }
}

回答1:


I wanted to read texts on my app. I found on Pub.dev flutter_tts: ^ 0.9.2 but the latest version flutter_tts 1.2.6 to date does not work for me at the moment. for now I don't dwell on it.

Here's how I did it: I changed minSdkVersion to 21 it is in the file: android/app/build.gradle

and for reading the texts:

//region [ FlutterTts ]
  FlutterTts flutterTts;
  dynamic languages;
  TtsState ttsState = TtsState.stopped;
  double _volume = 1.0;
  double _pitch = 1.3;
  double _rate = 0.6;
  String _newVoiceText;
  bool _isShowProgressVoiceRead=false;
  //endregion

@override
  void initState() {
    super.initState();
    initTts();
  }

//region [ TTS ]
  initTts() {
    flutterTts = FlutterTts();

    //_getLanguages();

    if (Platform.isAndroid) {
      _getEngines();
    }

    flutterTts.setStartHandler(() {
      setState(() {
        print("Playing");
        ttsState = TtsState.playing;
      });
    });

    flutterTts.setCompletionHandler(() {
      setState(() {
        print("Complete");
        ttsState = TtsState.stopped;
      });
    });

    /*flutterTts.setCancelHandler(() {
      setState(() {
        print("Cancel");
        ttsState = TtsState.stopped;
      });
    });*/

    if (kIsWeb || Platform.isIOS) {
      /* flutterTts.setPauseHandler(() {
        setState(() {
          print("Paused");
          ttsState = TtsState.paused;
        });
      });*/

      /*flutterTts.setContinueHandler(() {
        setState(() {
          print("Continued");
          ttsState = TtsState.continued;
        });
      });*/
    }

    flutterTts.setErrorHandler((msg) {
      setState(() {
        print("error: $msg");
        ttsState = TtsState.stopped;
      });
    });
  }

  /*Future _getLanguages() async {
    languages = await flutterTts.getLanguages;
    Tools.logCat('laguage: $languages');
    if (languages != null) setState(() => languages);
  }*/

  Future _getEngines() async {
    /*var engines = await flutterTts.getEngines;
    if (engines != null) {
      for (dynamic engine in engines) {
        print(engine);
      }
    }*/
  }

  Future startLecture() async {
    setState(() => _isShowProgressVoiceRead=true);
    await flutterTts.setVolume(_volume);
    await flutterTts.setSpeechRate(_rate);
    await flutterTts.setPitch(_pitch);

    flutterTts.setLanguage(getLanguageForTTS(langue: _preferredLanguage, question: question));

    _newVoiceText = 'VOTRE TEXT A LIRE';

    if (_newVoiceText != null) {
      if (_newVoiceText.isNotEmpty) {
        var result = await flutterTts.speak(_newVoiceText);
        if (result == 1) setState(() {
          ttsState = TtsState.playing;
          _isShowProgressVoiceRead=false;
        });
      }
    }
  }

  Future stopLecture() async {
    var result = await flutterTts.stop();
    if (result == 1) setState(() => ttsState = TtsState.stopped);
  }

  //endregion

Screen of my App... disponible sur Google Store https://play.google.com/store/apps/details?id=net.braindev.lasaintebible



来源:https://stackoverflow.com/questions/61386113/flutter-plugin-constructor-in-class-cannot-be-applied-to-given-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!