My goal is to create a real-time voice assistant with the following features. (Currently, it is a demo)
Whenever I use the app, I want it to listen
The app listens to what I say perfectly and updates the text based on what I say
As it is a demo, I have set the answer to "You said: $input. This is a dummy response." so it responds to my question.
If I say something, then it will listen to me properly, but when it speaks for a reply, then it will take its own voice, so after that, it will loop.
I would like the assistant to listen and answer after I speak, and in between when I speak and the assistant speaks, the assistant should stop and listen to what I say and answer according to the latest conversation.
I have attached my code here
import 'package:flutter/material.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;
import 'package:flutter_tts/flutter_tts.dart';
void main() => runApp(VoiceAssistantApp());
class VoiceAssistantApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: VoiceAssistantScreen(),
);
}
}
class VoiceAssistantScreen extends StatefulWidget {
@override
_VoiceAssistantScreenState createState() => _VoiceAssistantScreenState();
}
class _VoiceAssistantScreenState extends State<VoiceAssistantScreen> {
stt.SpeechToText _speech = stt.SpeechToText();
FlutterTts _flutterTts = FlutterTts();
bool _isListening = false;
String _lastWords = '';
String _response = '';
@override
void initState() {
super.initState();
startListening();
}
void startListening() async {
bool available = await _speech.initialize();
if (available) {
setState(() => _isListening = true);
_speech.listen(
onResult: (result) {
if (result.finalResult) {
setState(() {
_lastWords = result.recognizedWords;
});
processUserInput(_lastWords);
}
},
listenFor: Duration(minutes: 5),
pauseFor: Duration(seconds: 5),
listenOptions: stt.SpeechListenOptions(
cancelOnError: false,
partialResults: true,
onDevice: false,
listenMode: stt.ListenMode.confirmation,
sampleRate: 0,
autoPunctuation: true,
enableHapticFeedback: false));
}
}
void processUserInput(String input) {
String response = "You said: $input. This is a dummy response.";
setState(() {
_response = response; // Update the response text
});
speak(response).then((_) {
startListening(); // Restart listening after speaking
});
}
Future<void> speak(String text) async {
await _flutterTts.speak(text); // Speak without waiting for completion
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Voice Assistant')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
_isListening ? 'Listening...' : 'Not Listening',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 20), // Add some spacing
Text(
'You said:',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
Text(
_lastWords,
style: TextStyle(fontSize: 16, color: Colors.blue),
),
SizedBox(height: 20), // Add some spacing
Text(
'Response:',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
Text(
_response,
style: TextStyle(fontSize: 16, color: Colors.green),
),
],
),
),
);
}
}