start method

Future<void> start()

Starts audio recording and processing with the Porcupine engine Throws a PorcupineException if there was a problem starting audio recording

Implementation

Future<void> start() async {
  if (_isListening) {
    return;
  }
  if (_porcupine == null || _voiceProcessor == null) {
    throw PorcupineInvalidStateException(
        "Cannot start Porcupine - resources have already been released");
  }

  if (await _voiceProcessor?.hasRecordAudioPermission() ?? false) {
    _voiceProcessor?.addFrameListener(_frameListener);
    _voiceProcessor?.addErrorListener(_errorListener);
    try {
      await _voiceProcessor?.start(
          _porcupine!.frameLength, _porcupine!.sampleRate);
    } on PlatformException catch (e) {
      throw PorcupineRuntimeException(
          "Failed to start audio recording: ${e.message}");
    }
  } else {
    throw PorcupineRuntimeException(
        "User did not give permission to record audio.");
  }

  _isListening = true;
}