Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rozoomcool opened this issue Oct 14, 2023 · 4 comments
Labels
question Further information is requested

Comments

@rozoomcool
Copy link

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
Copy link
Collaborator

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

@sergsavchuk sergsavchuk added the question Further information is requested label Oct 29, 2023
@MohanadDaDev
Copy link

MohanadDaDev commented Dec 1, 2023

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
Copy link

andyyapwl commented Dec 30, 2023

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
Copy link

dnsprado commented Mar 7, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

5 participants