alphacep / vosk-flutter

Apache License 2.0
50 stars 30 forks source link

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

Open rozoomcool opened 8 months ago

rozoomcool commented 8 months ago

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

sergsavchuk commented 8 months ago

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

MohanadDaDev commented 7 months ago

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

andyyapwl commented 6 months ago

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

dnsprado commented 4 months ago

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.