Flutter Sound Plugin (Audio Recorder & Player)

Hyo
CodeChai
Published in
3 min readAug 20, 2018

I am happy to announce that flutter_sound plugin is out today. Please try version above 1.0.6.

What this module can do for ease of development

  1. Supports playing audio in background.
  2. Able to grab the actual uri of audio file to later upload to server.
  3. Able to run audio in silent mode which most apps support in default. Note that I haven’t support this optionally.
  4. Able to implement the callback status of your recording or playing file.
  5. Supports handling audio in different thread so that your UI will not freeze while using the module in your app.
  6. Audio will be recorded in maximum volume as default. For default in ios, recorded audio tends to be really small.

Things to know to help you understand about recording and playing for each platform.

Most popular extension today for audio is mp3. However, each platform share different file format for audio. For android it is mp4, and for ios it is a m4a. You could change the format to mp3 in your device with supports of third party libraries that exist in each platform, however it isn’t 100% trusted in my sight of view. Also there was some delay in converting audio that may result in bad user experience. Therefore, I’ve chose not to do work in converting in device and rather grab that file and do some job in server side would be better.

Why this plugin is recommended

  1. You can save your time from finding out how each platform works differently in recording or playing.
  2. You can save your time working on supporting your needs in development such as playing audio in background mode or recording in maximum volume as default. I don’t understand why it isn’t default in ios.
  3. I want to hear more needs from you to upgrade this module. Please write up any issue.
  4. Audio Recorder and Audio Player is at one place.

Lets try this out

Add plugin

dependencies:   flutter_sound: ^1.0.6

Install it

flutter packages get

Import it

import 'package:flutter_sound/flutter_sound.dart';

Usage

Creating instance.

FlutterSound flutterSound = new FlutterSound();

Starting recorder with listener.

String path = await flutterSound.startPlayer(null);
_playerSubscription = flutterSound.onPlayerStateChanged.listen((e) {
if (e != null) {
DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
String txt = DateFormat(‘mm:ss:SS’, ‘en_US’).format(date);
this.setState(() {
this._isPlaying = true;
this._playerTxt = txt.substring(0, 8);
});
}
});

Stop recorder

String result = await flutterSound.stopRecorder();
print(‘stopRecorder: $result’);
if (_recorderSubscription != null) {
_recorderSubscription.cancel();
_recorderSubscription = null;
}

Start player

String path = await flutterSound.startPlayer(null);
_playerSubscription = flutterSound.onPlayerStateChanged.listen((e) {
if (e != null) {
DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
String txt = DateFormat(‘mm:ss:SS’, ‘en_US’).format(date);
this.setState(() {
this._isPlaying = true;
this._playerTxt = txt.substring(0, 8);
});
}
});

Stop player

String result = await flutterSound.stopPlayer();
if (_playerSubscription != null) {
_playerSubscription.cancel();
_playerSubscription = null;
}

Pause player

String result = await flutterSound.pausePlayer();

Resume player

String result = await flutterSound.resumePlayer();

Seek player

String result = await flutterSound.seekToPlayer(miliSecs);

Setting the subscription duration (Optional). 0.01 is the default value when not set.

flutterSound.setSubscriptionDuration(0.01);

Setting volume

String path = await flutterSound.startPlayer(null);
await flutterSound.setVolume(0.1);

The Flutter Pub is a medium publication to bring you the latest and amazing resources such as articles, videos, codes, podcasts etc. about this great technology to teach you how to build beautiful apps with it. You can find us on Facebook, Twitter, and Medium or learn more about us here. We’d love to connect! And if you are a writer interested in writing for us, then you can do so through these guidelines.

--

--