vosk-flutter icon indicating copy to clipboard operation
vosk-flutter copied to clipboard

Unhandled Exception: PlatformException(INITIALIZE_FAIL, SpeechService instance already exist., null, null)

Open rozoomcool opened this issue 1 year ago • 4 comments

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().

rozoomcool avatar Oct 14 '23 17:10 rozoomcool

Hi! Please attach the code you are running. Are you actually doing a "hot restart" or a "hot reload"?

sergsavchuk avatar Oct 29 '23 21:10 sergsavchuk

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()))

MohanadDaDev avatar Dec 01 '23 20:12 MohanadDaDev

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(); } }

andyyapwl avatar Dec 30 '23 12:12 andyyapwl

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.

dnsprado avatar Mar 07 '24 14:03 dnsprado