baidu_speech_recognition 0.1.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new72

Flutter Baidu Speech Recognition Plugin

pub

A speech recognition plugin for flutter using BaiduSDK.See the changelog for more information about the function.

TODO

  • [x] iOS support
  • [ ] Android support

Getting Started

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

dependencies:
    baidu_speech_recognition: x.x.x

Basic Usage

import 'package:baidu_speech_recognition_example/speech_app.dart';

BaiduSpeechRecognition _speechRecognition = BaiduSpeechRecognition();

// initialize 
_speechRecognition.init().then((value) => print(value));
 
// start long speech recognition 
 _speechRecognition.startLongSpeech().then((value) => print(value)); 

 // start speech recognition 60s long
 _speechRecognition.start().then((value) => print(value));  

// cancel recognition 
 _speechRecognition.cancel().then((value) => print(value));
  

The Callback Listener

You can add a listener :

_speechRecognitoin.speechRecognitionEvents
      .listen((String value) {
        // TODO do somethig with the value
      }

The return value is a JSON String :

{
  "type": "The recognition result type",
  "value": "The result"
}

the type have the following value:

typedesc
readyready to speaking...
startdetect start speaking...
stopstop speaking. and return the last result
cancelcancel the last recognition
finishreturn the last recognition
lfinishlong speech return
endend speaking...
meterreturn volume meter level

For iOS developer

Go to 百度ASR download SDK for iOS,then copy BDSClientLib and BDSClientResource to the same directory of you flutter project,the file structure like this:

----------------
  |
  |--Your FLutter Projcet/
  |
  |--BDSClientLib/
  |
  |--BDSCLientResource/

Then open your iOS projcet on Xcode and add the baidu speech SDK library and some resource.

Add BDSClientLib/libBaiduSpeechSDK.a to you project group as "create groups",

Add BDSClientResource/ASR/BDSClientResources to your project group as "create folder references",

Add BDSClientResource/ASR/BDSClientEASRResources to your project group as "create groups".

Add the follow framework to your project:

FrameworkDesc
libc++.tbdFor c/c++ func support
libz.1.2.5.tbdFor gzip support
libsqlite3.0.tbdFor sqlite support
libiconv.2.4.0.tbdSome utility

Finally add Microphone Usage privacy to your info.plist file.

Project Setting

Open you project with xcode and go to Pods, select the baidu_speech_recognition TARGETS, then select the Build Settings Tab, Change Mach-O Type to Static Library.Then go to the Build Phases, make sure all the Headers are Public.

If you have any problem or Error Please make a issue.

0.0.1

  • 60s 短语音 识别
  • 长语音识别
  • iOS 支持

example/lib/main.dart

import 'package:flutter/material.dart';

import 'package:baidu_speech_recognition/baidu_speech_recognition.dart';
import 'dart:convert';
import 'dart:async';

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


class MyApp extends StatefulWidget {

  @override
  _MyAppState createState() => new _MyAppState();

}

enum MenuItem { longSpeech }

class _MyAppState extends State<MyApp> {


  Map<String, dynamic> _recResult;

  BaiduSpeechRecognition _speechRecognition = BaiduSpeechRecognition();

  bool isStart = false;
  bool isLongSpeech = false;
  
  List<String> results = List();

  int meterLevel = 0;

  StreamSubscription<dynamic> _speechEvents;
  String status = 'Tap To Speaking...';

  final List<String> icons = <String>[

    'assets/images/meter_level_0.png',
    'assets/images/meter_level_1.png',
    'assets/images/meter_level_2.png',
    'assets/images/meter_level_3.png',
    'assets/images/meter_level_4.png',
    'assets/images/meter_level_5.png',
    'assets/images/meter_level_6.png',

  ];


  Widget _buildPopupMenu() {
    return new PopupMenuButton<MenuItem>(
      onSelected: (MenuItem result) async {
        switch (result) {
          case MenuItem.longSpeech:
            _onLongSpeechChange(!isLongSpeech);
            break;
        }
      },
      itemBuilder: (BuildContext context) => <PopupMenuEntry<MenuItem>>[
        PopupMenuItem<MenuItem>(
          value: MenuItem.longSpeech,
          child: Row(
            children: <Widget>[
              Checkbox(
                value: isLongSpeech,
                onChanged: _onLongSpeechChange,
              ),

              Text('长语音识别'),
            ],
          ),
        )
      ],
    );
  }
  
  Widget _buildRecognitionResultItem(BuildContext context, int index) {
    
    return Column(

        crossAxisAlignment: CrossAxisAlignment.end,

        children: <Widget>[

          Container(

            margin: EdgeInsets.all(5.0),
            padding: EdgeInsets.all(5.0),

            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(4.0)),
              color: Colors.blue
            ),

            child: Text(
                results[index]
            ),

          )


        ],

    );
    
  }

  _startSpeechRecognition() {

    if (!isStart) {

      if (isLongSpeech) {
        _speechRecognition.startLongSpeech().then((value) => print(value));
      } else {
        _speechRecognition.start().then((value) => print(value));
      }
      isStart = true;

    } else {

      _speechRecognition.stop().then((value) => print(value));
      isStart = false;

    }

    setState(() {

    });

  }

  _onLongSpeechChange(bool value) {

    setState(() {
      isLongSpeech = value;
    });

  }

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

    // 初始化
    _speechRecognition.init().then((value) => print(value));

    _speechEvents = _speechRecognition.speechRecognitionEvents
      .listen((String value) {

        if (value != null) {

        _recResult = jsonDecode(value);
        print(_recResult);

          setState(() {

            switch (_recResult['type']) {
              case 'meter':
                meterLevel = _recResult['value'];
                break;
              case 'ready':
                status = 'ready...';
                break;
              case 'start':
                status = 'speaking...';
                break;
              case 'finish':
                results.add(_recResult['value']['results_recognition'][0]);
                isStart = false;
                meterLevel = 0;
                break;
              case 'lfinish':
                results.add(_recResult['value']['results_recognition'][0]);
                isStart = false;
                meterLevel = 0;
                break;
              case 'end':
                meterLevel = 0;
                isStart = false;
                break;
              default:
                print(_recResult);
                break;
            }

          });
        }

    });
  }

  @override
  Widget build(BuildContext context) {

    int icon;

    if (meterLevel <= 0) {
      icon = 0;
    } else if (meterLevel <= 4) {
      icon = 1;
    } else if (meterLevel <= 20) {
      icon = 2;
    } else if (meterLevel <= 36) {
      icon = 3;
    } else if (meterLevel <= 52) {
      icon = 4;
    } else if (meterLevel <= 68) {
      icon = 5;
    } else {
      icon = 6;
    }

    print('$icon');

    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(

          title: const Text('Baidu Speech Plugin'),

          actions: <Widget>[

            _buildPopupMenu()

          ],


        ),

        body: new Container(

            padding: EdgeInsets.all(10.0),

            child:  Column(

              children: <Widget>[

                Expanded(child: ListView.builder(

                  itemBuilder: _buildRecognitionResultItem,
                  itemCount: results.length,
                  //reverse: true,

                )),

                Container(height: 4.0),

                Material(

                   elevation: 3.0,
                   borderRadius: BorderRadius.all(Radius.circular(4.0)),
                   child: IconButton(
                     onPressed: _startSpeechRecognition,

                     icon: Image(image: AssetImage(icons[icon])),
                     //color: Colors.blue,
                     tooltip: 'tap to speaking....',

                   ),

                ),

                Padding(
                  
                  padding: EdgeInsets.all(10.0),
                  
                  child: Text(

                      status

                  ),
                  
                )
                ,

              ],

            )
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  baidu_speech_recognition: ^0.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:baidu_speech_recognition/baidu_speech_recognition.dart';
  
Version Uploaded Documentation Archive
0.1.1 Aug 30, 2018 Go to the documentation of baidu_speech_recognition 0.1.1 Download baidu_speech_recognition 0.1.1 archive
0.1.0 Aug 29, 2018 Go to the documentation of baidu_speech_recognition 0.1.0 Download baidu_speech_recognition 0.1.0 archive
0.0.2 Aug 27, 2018 Go to the documentation of baidu_speech_recognition 0.0.2 Download baidu_speech_recognition 0.0.2 archive
0.0.1 Aug 27, 2018 Go to the documentation of baidu_speech_recognition 0.0.1 Download baidu_speech_recognition 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
51
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
72
Learn more about scoring.

We analyzed this package on Sep 18, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.3
  • Flutter: 0.8.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

The description is too short.

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Format lib/baidu_speech_recognition.dart.

Run flutter format to format lib/baidu_speech_recognition.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8