vosk_flutter 0.3.46 copy "vosk_flutter: ^0.3.46" to clipboard
vosk_flutter: ^0.3.46 copied to clipboard

outdated

Flutter plugin for Vosk speech recognition model.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:vosk_flutter/vosk_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: VoskFlutterDemo(),
    );
  }
}

class VoskFlutterDemo extends StatefulWidget {
  const VoskFlutterDemo({Key? key}) : super(key: key);

  @override
  State<VoskFlutterDemo> createState() => _VoskFlutterDemoState();
}

class _VoskFlutterDemoState extends State<VoskFlutterDemo> {
  static const _textStyle = TextStyle(fontSize: 30, color: Colors.black);
  static const _modelName = 'vosk-model-small-en-us-0.15';
  static const _sampleRate = 16000;

  final _vosk = VoskFlutterPlugin.instance();
  final _modelLoader = ModelLoader();

  String? _error;
  Model? _model;
  Recognizer? _recognizer;
  SpeechService? _speechService;

  bool _recognitionStarted = false;

  @override
  void initState() {
    super.initState();

    _modelLoader
        .loadModelsList()
        .then((modelsList) =>
            modelsList.firstWhere((model) => model.name == _modelName))
        .then((modelDescription) =>
            _modelLoader.loadFromNetwork(modelDescription.url)) // load model
        .then(
            (modelPath) => _vosk.createModel(modelPath)) // create model object
        .then((model) => setState(() => _model = model))
        .then((_) => _vosk.createRecognizer(
            model: _model!, sampleRate: _sampleRate)) // create recognizer
        .then((value) => _recognizer = value)
        .then((recognizer) =>
            _vosk.initSpeechService(_recognizer!)) // init speech service
        .then((speechService) => setState(() => _speechService = speechService))
        .catchError((e) => setState(() => _error = e.toString()));
  }

  @override
  Widget build(BuildContext context) {
    if (_error != null) {
      return Scaffold(
          body: Center(child: Text("Error: $_error", style: _textStyle)));
    } else if (_model == null) {
      return const Scaffold(
          body: Center(child: Text("Loading model...", style: _textStyle)));
    } else if (_speechService == null) {
      return const Scaffold(
        body: Center(
          child: Text("Initializing speech service...", style: _textStyle),
        ),
      );
    } else {
      return Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                  onPressed: () async {
                    if (_recognitionStarted) {
                      await _speechService!.stop();
                    } else {
                      await _speechService!.start();
                    }
                    setState(() => _recognitionStarted = !_recognitionStarted);
                  },
                  child: Text(_recognitionStarted
                      ? "Stop recognition"
                      : "Start recognition")),
              StreamBuilder(
                  stream: _speechService!.onPartial(),
                  builder: (context, snapshot) => Text(
                      "Partial result: ${snapshot.data.toString()}",
                      style: _textStyle)),
              StreamBuilder(
                  stream: _speechService!.onResult(),
                  builder: (context, snapshot) => Text(
                      "Result: ${snapshot.data.toString()}",
                      style: _textStyle)),
            ],
          ),
        ),
      );
    }
  }
}
copied to clipboard
20
likes
0
points
385
downloads

Publisher

unverified uploader

Weekly Downloads

2024.05.28 - 2025.04.22

Flutter plugin for Vosk speech recognition model.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

archive, flutter, http, path, path_provider, permission_handler

More

Packages that depend on vosk_flutter