flutter_plugin_record 0.1.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 89

[TOC]

使用Flutter实现 仿微信录音的插件 #

插件支持android 和IOS


插件提供的功能

  1. 录制语音,
  2. 播放录音,
  3. 录制声音大小的监听
  4. 提供录制时长的监听
  5. 提供类似微信的录制组件
  6. 提供播放音频结束的监听

1,引入 #

在pubspec.yaml 文件上引入如下配置

引入方式1(引入最新的版本)
flutter_plugin_record:
    git:
      url: https://github.com/yxwandroid/flutter_plugin_record.git

引入方式2 (引入指定某次commit)
flutter_plugin_record:
    git:
      url: https://github.com/yxwandroid/flutter_plugin_record.git
      ref: 29c02b15835907879451ad9f8f88c357149c6085
      
引入方式3 (引入Flutter仓库的library)
      
dependencies:
  flutter_plugin_record: ^0.0.9
          
          
      
      
      
      
    

使用 #

1, 初始化录制 #

可以在页面初始化的时候进行初始化比如: 在initState方法中进行初始化

//实例化对象 
FlutterPluginRecord   recordPlugin = new FlutterPluginRecord();
//    初始化
recordPlugin.init()

2, 开始录制 #

 recordPlugin.start()

3, 停止录制 #

 recordPlugin.stop()

4, 播放 #

 recordPlugin.play()

3, 释放资源 #

可以在页面退出的时候进行资源释放 比如在 dispose方法中调用如下代码

 recordPlugin.dispose()
 

4,回调监听 #

1,初始化回调监听

///初始化方法的监听
recordPlugin.responseFromInit.listen((data) {
  if (data) {
    print("初始化成功");
  } else {
    print("初始化失败");
  }
});

2,开始录制停止录制监听

 /// 开始录制或结束录制的监听
    recordPlugin.response.listen((data) {
      if (data.msg == "onStop") {
        ///结束录制时会返回录制文件的地址方便上传服务器
        print("onStop  " + data.path);
      } else if (data.msg == "onStart") {
        print("onStart --");
      }
    });

3,录制声音大小回调监听

 ///录制过程监听录制的声音的大小 方便做语音动画显示图片的样式
    recordPlugin.responseFromAmplitude.listen((data) {
      var voiceData = double.parse(data.msg);
      var tempVoice = "";
      if (voiceData > 0 && voiceData < 0.1) {
        tempVoice = "images/voice_volume_2.png";
      } else if (voiceData > 0.2 && voiceData < 0.3) {
        tempVoice = "images/voice_volume_3.png";
      } else if (voiceData > 0.3 && voiceData < 0.4) {
        tempVoice = "images/voice_volume_4.png";
      } else if (voiceData > 0.4 && voiceData < 0.5) {
        tempVoice = "images/voice_volume_5.png";
      } else if (voiceData > 0.5 && voiceData < 0.6) {
        tempVoice = "images/voice_volume_6.png";
      } else if (voiceData > 0.6 && voiceData < 0.7) {
        tempVoice = "images/voice_volume_7.png";
      } else if (voiceData > 0.7 && voiceData < 1) {
        tempVoice = "images/voice_volume_7.png";
      }
      setState(() {
        voiceIco = tempVoice;
        if(overlayEntry!=null){
          overlayEntry.markNeedsBuild();
        }
      });

      print("振幅大小   " + voiceData.toString() + "  " + voiceIco);
    });







   

4,播放声音完成的监听监听

  recordPlugin.responsePlayStateController.listen((data){
  print("播放路径   " + data.playPath );
  print("播放状态   " + data.playState );
});



2,录制组件的使用 #

组件使用效果

android效果

IOS效果

1,在使用的页面进行导入package #

import 'package:flutter_plugin_record/index.dart';  
    




2,在使用的地方引入VoiceWidget组件 #

VoiceWidget(),


VoiceWidget({startRecord: Function, stopRecord: Function}) {



startRecord 开始录制的回调

stopRecord 停止录制的回调 返回的path是录制成功之后文件的保存地址

IOS配置注意事项 #

ios集成的的时候需要在info.list添加 #

 <key>NSMicrophoneUsageDescription</key>
        <string>打开话筒</string>
 

更新内容 #

  • [x] 实现录制时长的监听
  • [x] 实现播放指定路径音频文件
  • [x] 实现播放音频完成的回调

TODO #

  • [ ] 实现发送语音时间按下抬起时间很短提示

关注公众号获取更多内容 #

0.1.3 #

  • 实现播放完成的回调监听

0.1.2 #

  • 实现播放指定路径录音文件

0.1.1 #

  • 格式代码

0.1.0 #

  • 修复提示bug

0.0.9 #

  • 添加记录录制时间的功能

0.0.8 #

  • 重构项目为oc项目 解决播放oc 工程无法使用问题

0.0.7 #

  • 更新readme

0.0.6 #

  • 适配android 9.0
  • 解决ios集成到oc项目不成功问题

0.0.5 #

  • 添加 readme 说明

0.0.4 #

  • 添加example readme

0.0.3 #

  • 添加引入方式

0.0.2 #

  • 添加license

0.0.1 #

  • 初始化项目

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_plugin_record_example/overlay_screen.dart';
import 'package:flutter_plugin_record_example/second_screen.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
      routes: {
        "SecondScreen": (BuildContext context) => new SecondScreen(),
        "OverLayScreen": (BuildContext context) => new OverLayScreen(),
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {


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

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("flutter版微信语音录制实现"),
      ),
      body: new Center(
        child: new Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            new FlatButton(
                onPressed: () {
                  Navigator.pushNamed<dynamic>(context, "SecondScreen");
                },
                child: new Text("进入语音录制界面")),
            new FlatButton(
                onPressed: () {
                  Navigator.pushNamed<dynamic>(context, "OverLayScreen");
                },
                child: new Text("进入仿微信录制界面")),

          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_plugin_record: ^0.1.3

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:flutter_plugin_record/flutter_plugin_record.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
78
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]
89
Learn more about scoring.

We analyzed this package on Apr 6, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Format lib/play_state.dart.

Run flutter format to format lib/play_state.dart.

Format lib/voice_widget.dart.

Run flutter format to format lib/voice_widget.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
uuid ^2.0.2 2.0.4
Transitive dependencies
charcode 1.1.3
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.4
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test