web_media_recorder 0.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new64

Web Media Recording #

A library to implement the browser MediaRecorder native features into the flutter web.

Usage #

  // Variables needed
  WebRecorder webRecorder;
  bool isRecording;
  
  // Usually initialized in initState
  webRecorder = WebRecorder(
    whenRecorderStart: whenRecorderStart,
    whenRecorderStop: whenRecorderStop,
    whenReceiveData: receiveData
  );
  
  // To be used to start/stop recording
  webRecorder.openRecorder(); 
  
  // A function that will return the audio data as Uint8List
  receiveData(data){
    // TODO Your logic to use this Uint8List audio data...
    print(data);
  }
  
  //Will give you the possibility to toggle a START/STOP Button
  whenRecorderStart(){
    print('Recorder Started');
    setState(() {}); 
  }
  whenRecorderStop(){
    print('Recorder Stopped'); 
    setState(() {}); 
  }
}

Installation #

Add this dependency your pubspec.yaml file:

dependencies:
  web_media_recorder: ^0.0.1

For more info visit pub.dev.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

If you wanna see my high level IQ functions go deeper #


import 'dart:async';
import 'dart:html' as html;
import 'dart:js' as js;

import 'package:flutter/material.dart';

class WebRecorder {
  static bool isNotRecording = true;
  static html.MediaRecorder recorder;

  final Function whenRecorderStart; // Function to call when recording starts
  final Function whenRecorderStop; // Function to call when recording finishs
  final Function whenReceiveData;

  WebRecorder({
      @required this.whenRecorderStart, 
      @required this.whenRecorderStop, 
      @required this.whenReceiveData
  });

  openRecorder(){
    WebRecorder.isNotRecording = !WebRecorder.isNotRecording;
    if(WebRecorder.isNotRecording)
      stopRecoring().whenComplete(whenRecorderStop);
    else
      html.window.navigator
      .getUserMedia(audio: true)
      .then((stream) {
          recorder = html.MediaRecorder(stream);
          recorder.addEventListener('dataavailable', hundlerFunctionStream);
      })
      .whenComplete((){
        startRecording().whenComplete(whenRecorderStart);
      })
      .catchError((e)=> print);
  }
  
  Future<void> startRecording(){
    WebRecorder.recorder.start();
    return Future.value(true);
  }

  Future<void> stopRecoring() async{
    WebRecorder.recorder.stop();
    return Future.value(true);
  }

  hundlerFunctionStream(event) async{
    html.FileReader reader = html.FileReader();
    html.Blob blob = js.JsObject.fromBrowserObject(event)['data'];
    reader.readAsArrayBuffer(blob);
    reader.onLoadEnd.listen((e) async {
      setData(reader.result);
    });
  }

  setData(data) => whenReceiveData(data);

  dispose(){
    WebRecorder.recorder.removeEventListener('dataavailable', hundlerFunctionStream);
    WebRecorder.recorder = null;
  }
}

Negative IQ right ? 😂 😂 #

Do not forget to pull request into the repo if you see there is some lines of code must be changed, It would be my pleasure ❤️

Getting Started #

This project is a starting point for a Dart package, a library module containing code that can be shared easily across multiple Flutter or Dart projects.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

[0.0.1] - Jan 28, 2020 10:30 #

  • Initial release.
  • Support WEB only
  • Add Audio Media Recording Feature

[0.1.0] - Jan 28, 2020 12:30 #

  • Bug fix
  • Add docs

example/main.dart

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

void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: WebMediaRecorder(),
    );
  }
}


class WebMediaRecorder extends StatefulWidget {
  @override
  _WebMediaRecorderState createState() => _WebMediaRecorderState();
}

class _WebMediaRecorderState extends State<WebMediaRecorder> {
  WebRecorder webRecorder;
  bool isRecording;
  
  @override
  void initState() {
    super.initState();
    webRecorder = WebRecorder(
      whenRecorderStart: whenRecorderStart,
      whenRecorderStop: whenRecorderStop,
      whenReceiveData: receiveData
    );
  }

  @override
  void dispose(){
    super.dispose();
    webRecorder.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: FlatButton(
          onPressed: () {
            webRecorder.openRecorder();
          },
          child: Text(
            !WebRecorder.isNotRecording ? "Stop":"Start",
            style: Theme.of(context).textTheme.display1
          ),
          color: Colors.blue,
          textColor: Colors.white,
          disabledColor: Colors.grey,
          disabledTextColor: Colors.black,
          padding: EdgeInsets.all(8.0),
          splashColor: Colors.blueAccent,
        ),
      ),
    );
  }

  receiveData(data){
    // TODO Your logic to use this Uint8List audio data
    print(data);
  }

  whenRecorderStart(){
    print('Recorder Started');
    setState(() {});
  }

  whenRecorderStop(){
    print('Recorder Stopped'); 
    setState(() {});
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  web_media_recorder: ^0.1.0

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

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

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

Health suggestions

Format lib/src/web_recorder.dart.

Run flutter format to format lib/src/web_recorder.dart.

Dependencies

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