speech_to_text 0.6.2

  • Readme
  • Changelog
  • Example
  • Installing
  • new82

speech_to_text #

A library that exposes device specific text to speech recognition capability.

This plugin contains a set of classes that make it easy to use the speech recognition capabilities of the mobile device in Flutter. It supports both Android and iOS.

Recent Updates #

The 0.6.0 version added a Duration parameter to the listen method to set a maximum time to listen before automatically cancelling.

Note: This plugin is under development and will be extended over the coming weeks.

Using #

To recognize text from the microphone import the package and call the plugin, like so:

import 'package:speech_to_text/speech_to_text.dart' as stt;

    stt.SpeechToText speech = stt.SpeechToText();
    bool available = await speech.initialize( onStatus: statusListener, onError: errorListener );
    if ( available ) {
        speech.listen( onResult: resultListener );
    }
    else {
        print("The user has denied the use of speech recognition.");
    }
    // some time later...
    speech.stop()

Permissions #

Applications using this plugin require user permissions.

iOS #

Add the following keys to your Info.plist file, located in <project root>/ios/Runner/Info.plist:

  • NSSpeechRecognitionUsageDescription - describe why your app uses speech recognition. This is called Privacy - Speech Recognition Usage Description in the visual editor.
  • NSMicrophoneUsageDescription - describe why your app needs access to the microphone. This is called Privacy - Microphone Usage Description in the visual editor.

Android #

Add the record audio permission to your AndroidManifest.xml file, located in <project root>/android/app/src/main/AndroidManifest.xml.

  • android.permission.RECORD_AUDIO - this permission is required for microphone access.
  • android.permission.INTERNET - this permission is required because speech recognition may use remote services.

Adding Sounds for iOS (optional) #

Android automatically plays system sounds when speech listening starts or stops but iOS does not. This plugin supports playing sounds to indicate listening status on iOS if sound files are available as assets in the application. To enable sounds in an application using this plugin add the sound files to the project and reference them in the assets section of the application pubspec.yaml. The location and filenames of the sound files must exactly match or they will not be found. The example application for the plugin shows the usage.

  assets:
  - assets/sounds/speech_to_text_listening.m4r
  - assets/sounds/speech_to_text_cancel.m4r
  - assets/sounds/speech_to_text_stop.m4r
  • speech_to_text_listening.m4r - played when the listen method is called.
  • speech_to_text_cancel.m4r - played when the cancel method is called.
  • speech_to_text_stop.m4r - played when the stop method is called.

Changelog #

0.6.2 #


Fix #

  • channel invoke wasn't being done on the main thread in iOS

0.6.1 #


Fix #

  • listening sound was failing due to timing, now uses play and record mode on iOS.

0.6.0 #


Breaking #

  • The filenames for the optional sounds for iOS have changed.

New #

  • Added an optional listenFor parameter to set a max duration to listen for speech and then automatically cancel.

Fix #

  • Was failing to play sounds because of record mode. Now plays sounds before going into record mode and after coming out.
  • Status listener was being ignored, now properly notifies on status changes.

0.5.1 #


  • Fixes a problem where the recognizer left the AVAudioSession in record mode which meant that subsequent sounds couldn't be played.

0.5.0 #


Initial draft with limited functionality, supports:

  • initializing speech recognition
  • asking the user for permission if required
  • listening for recognized speech
  • canceling the current recognition session
  • stopping the current recognition session
  • Android and iOS 10+ support

Missing:

  • some error handling
  • testing across multiple OS versions
  • and more, to be discovered...

example/README.md

speech_to_text_example #

Demonstrates how to use the speech_to_text plugin. This example requires that the plugin has been installed. It initializes speech recognition, listens for words and prints them.

Source #

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

import 'package:speech_to_text/speech_to_text.dart';
import 'package:speech_to_text/speech_recognition_result.dart';
import 'package:speech_to_text/speech_recognition_error.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _hasSpeech = false;
  String lastWords = "";
  String lastError = "";
  String lastStatus = "";
  final SpeechToText speech = SpeechToText();

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

  Future<void> initSpeechState() async {
    bool hasSpeech = await speech.initialize(onError: errorListener, onStatus: statusListener );

    if (!mounted) return;
    setState(() {
      _hasSpeech = hasSpeech;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Speech to Text Example'),
        ),
        body: _hasSpeech
            ? Column(children: [
                Expanded(
                  child: Center(
                    child: Text('Speech recognition available'),
                  ),
                ),
                Expanded(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      FlatButton(
                        child: Text('Start'),
                        onPressed: startListening,
                      ),
                      FlatButton(
                        child: Text('Stop'),
                        onPressed: stopListening,
                      ),
                      FlatButton(
                        child: Text('Cancel'),
                        onPressed:cancelListening,
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Column(
                    children: <Widget>[
                      Center(
                        child: Text('Recognized Words'),
                      ),
                      Center(
                        child: Text(lastWords),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Column(
                    children: <Widget>[
                      Center(
                        child: Text('Error'),
                      ),
                      Center(
                        child: Text(lastError),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Center(
                    child: speech.isListening ? Text("I'm listening...") : Text( 'Not listening' ),
                  ),
                ),
              ])
            : Center( child: Text('Speech recognition unavailable', style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold))),
      ),
    );
  }

  void startListening() {
    lastWords = "";
    lastError = "";
    speech.listen(onResult: resultListener );
    setState(() {
      
    });
  }

  void stopListening() {
    speech.stop( );
    setState(() {
      
    });
  }

  void cancelListening() {
    speech.cancel( );
    setState(() {
      
    });
  }

  void resultListener(SpeechRecognitionResult result) {
    setState(() {
      lastWords = "${result.recognizedWords} - ${result.finalResult}";
    });
  }

  void errorListener(SpeechRecognitionError error ) {
    setState(() {
      lastError = "${error.errorMsg} - ${error.permanent}";
    });
  }
  void statusListener(String status ) {
    setState(() {
      lastStatus = "$status";
    });
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  speech_to_text: ^0.6.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:speech_to_text/speech_to_text.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
63
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Oct 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
json_annotation ^3.0.0 3.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
build_runner ^1.0.0
flutter_test
json_serializable ^3.0.0