vosk-flutter
vosk-flutter copied to clipboard
Unhandled Exception: PlatformException(INITIALIZE_FAIL, SpeechService instance already exist., null, null)
When using the library in flutter everything works fine, but when I do a hot restart, suddenly this error occurs. I initialize the speech service using vosk.initSpeechService() in a separate method that I run in initState().
Hi! Please attach the code you are running. Are you actually doing a "hot restart" or a "hot reload"?
this error happens on the package code example, I think it's related to not disposing the service and the recognizer, but I even tried to dispose them in the dispose() method and the error still happens. (on hot restart because that's when the initService is called (inside initState()))
For sake of time, I modified the existing class and added speechService and getter for my screen to access:
class VoskFlutterPlugin { ... static SpeechService? _speechService;
Future<SpeechService?> initSpeechService(Recognizer recognizer) async { if(_speechService!=null) return _speechService;
if (await Permission.microphone.status == PermissionStatus.denied &&
await Permission.microphone.request() == PermissionStatus.denied) {
throw MicrophoneAccessDeniedException();
}
await _channel.invokeMethod('speechService.init', {
'recognizerId': recognizer.id,
'sampleRate': recognizer.sampleRate,
});
_speechService = SpeechService(_channel);
return _speechService;
}
SpeechService? getSpeechService() { if(_speechService==null) _speechService = SpeechService(_channel); return _speechService; }
My project code: void initialize() async { try { _speechService = await _vosk.initSpeechService(_recognizer!); // init speech service } catch (ex) { print("Error in initializing speechservice: ${ex}. Manually access from getter property vosk.getSpeechService."); _speechService = _vosk.getSpeechService(); } }
For sake of time, I modified the existing class and added speechService and getter for my screen to access:
class VoskFlutterPlugin { ... static SpeechService? _speechService;
Future<SpeechService?> initSpeechService(Recognizer recognizer) async { if(_speechService!=null) return _speechService;
if (await Permission.microphone.status == PermissionStatus.denied && await Permission.microphone.request() == PermissionStatus.denied) { throw MicrophoneAccessDeniedException(); } await _channel.invokeMethod('speechService.init', { 'recognizerId': recognizer.id, 'sampleRate': recognizer.sampleRate, }); _speechService = SpeechService(_channel); return _speechService;
}
SpeechService? getSpeechService() { if(_speechService==null) _speechService = SpeechService(_channel); return _speechService; }
My project code: void initialize() async { try { _speechService = await _vosk.initSpeechService(_recognizer!); // init speech service } catch (ex) { print("Error in initializing speechservice: ${ex}. Manually access from getter property vosk.getSpeechService."); _speechService = _vosk.getSpeechService(); } }
Thanks for your solution @andyyapwl , it worked fine.