flutterPorcupine — Flutter Quick Start

Platforms

  • Flutter (1.20.0+)
  • Android (4.4+, API 19+)
  • iOS (9.0+)

Requirements

Picovoice Account & AccessKey

Signup or Login to Picovoice Console to get your AccessKey. Make sure to keep your AccessKey secret.

Quick Start

Setup

  1. Install Flutter SDK.

  2. Run flutter doctor to determine any missing requirements.

  3. Add the Porcupine plugin to your app project by referencing it in pubspec.yaml:

dependencies:
porcupine_flutter: ^<version>
  1. Enable the proper permission for recording with the hardware's microphone on both iOS and Android:

iOS

Open your Info.plist and add the following line:

<key>NSMicrophoneUsageDescription</key>
<string>[Permission explanation]</string>


Android

Open your AndroidManifest.xml and add the following line:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />


Usage

Create an instance of PorcupineManager using the constructor PorcupineManager.fromKeywords that detects the included built-in wake words porcupine and bumblebee:

import 'package:porcupine/porcupine_manager.dart';
import 'package:porcupine/porcupine_error.dart';
const accessKey = "{ACCESS_KEY}"
void createPorcupineManager() async {
try{
_porcupineManager = await PorcupineManager.fromBuiltInKeywords(
"{ACCESS_KEY}",
[BuiltInKeyword.PORCUPINE, BuiltInKeyword.BUMBLEBEE],
_wakeWordCallback);
} on PorcupineException catch (err) {
// handle porcupine init error
}
}

The _wakeWordCallback parameter is a function that is invoked when Porcupine detects the wake word:

void _wakeWordCallback(int keywordIndex){
if(keywordIndex === 0){
// porcupine detected
}
else if (keywordIndex === 1){
// bumblebee detected
}
}

Start audio capture and wake word detection:

try{
await _porcupineManager.start();
} on PvAudioException catch (ex) {
// deal with audio exception
}

Stop with:

await _porcupineManager.stop();

Release resources explicitly when done with Porcupine:

await _porcupineManager.delete();

Custom Keywords

  1. Create custom keywords using the Picovoice Console.
  2. Download the custom wake word file (.ppn).
  3. Add the file to the assets folder in the project directory.
  4. Add the relative path to the pubspec.yaml:
flutter:
assets:
- assets/${KEYWORD_FILE}
  1. Use the PorcupineManager.fromKeywordPaths static constructor and provide the paths to the .ppn file(s):
String keywordAsset = "assets/${KEYWORD_FILE}"
_porcupineManager = await PorcupineManager.fromKeywordPaths(
"${ACCESS_KEY}",
["assets/${KEYWORD_FILE}"],
_wakeWordCallback);

Non-English Languages

Use the corresponding model file (.pv) to detect non-English wake words. The model files for all supported languages are available on the Porcupine GitHub repository.

  1. Add the file to the assets folder in the project directory.
  2. Add it to the pubspec.yaml:
flutter:
assets:
- assets/${KEYWORD_FILE}
- assets/${MODEL_FILE}

Pass in the model file using the modelPath parameter to change the detection language:

_porcupineManager = await PorcupineManager.fromKeywordPaths(
"{ACCESS_KEY}",
"assets/${KEYWORD_FILE}",
_wakeWordCallback,
modelPath: "assets/${MODEL_FILE}");

Demo

For the Porcupine Flutter SDK, we offer demo applications that demonstrate how to use the Wake Word engine on real-time audio streams (i.e. microphone input).

Setup

Clone the Porcupine GitHub Repository:

git clone --recurse-submodules https://github.com/Picovoice/porcupine.git

Usage

  1. Replace {YOUR_ACCESS_KEY_HERE} with a valid AccessKey in the demo/flutter/lib/main.dart file:

  2. Build and deploy the demo to your device:

cd porcupine/demo/flutter
flutter run

For more information on our Porcupine demos for Flutter, head over to our GitHub repository.

Resources

Packages

API

GitHub

Benchmark

Further Reading

Video


Issue with this doc? Please let us know.