我希望能够通过语音控制我的应用。 问题是,我今天在pub.dev和论坛中发现的solutiosn以这种方式工作
我需要的是这样
基本上,我想要诸如“ Hi siri”,“ ok google”之类的交互功能,而不是想要“ pause”
谁能推荐解决方案
答案 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)