使用Kotlin开发Flutter TTS插件。
我遇到的一个问题是onInit没有触发。
package com.nannex.flutterplugin
import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.util.Log
import java.util.*
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.PluginRegistry.Registrar
class FlutterPlugin() : MethodCallHandler, TextToSpeech.OnInitListener {
var test: String = "not worked";
var tts: TextToSpeech? = null;
override fun onInit(status: Int) {
if (status != TextToSpeech.ERROR) {
tts!!.setLanguage(Locale.US)
test = "worked";
}
}
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "flutter_plugin")
channel.setMethodCallHandler(FlutterPlugin())
}
}
override fun onMethodCall(call: MethodCall, result: Result): Unit {
if (call.method.equals("getPlatformVersion")) {
result.success(test)
} else {
result.notImplemented()
}
}
fun speak(text: String) {
tts!!.speak(text, TextToSpeech.QUEUE_FLUSH, null, "")
}
}
不确定在这里我在做什么错。有人可以指出如何启动Init的正确方向吗?据我了解,它应该是OnInitListener的回调,但似乎没有用。
我还以以下为指导。