SoundRecorder

Overview

The SoundRecorder class provides an api for recording audio.
If you need a widget then you should be using SoundRecorderUI.
The SoundRecorder class records audio into a Track.
As of this time the SoundRecorder can only record into a Track that has been created using Track.fromFile() and the MediaFormat must be a natively supported format.

MediaFormat selection

The Track defines the MediaFormat that will be used to record the audio. You should select the MediaFormat carefully. AdtsAacMediaFormat is generally the best chooice as it is supported on both platforms.

Native MediaFormat

Each platform (Android/iOS) natively supports a small set of MediaFormat. If you choose a non native MediaFormat then Sounds throw a MediaFormatException.
When creating a Track to record to you must specify the MediaFormat. You can either directly use one of the MediaFormats derived from NativeMediaFormat or use the convenience class WellKnowMediaFormats.
The set of supported NativeMediaFormat classes are:
    MediaFormat
    AdtsAacMediaFormat
    OggOpusMediaFormat
    CafOpusMediaFormat
    Mp3MediaFormat
    OggVorbisMediaFormat
    PCMMediaFormat
The WellKnownMediaFormats classes exposes the following properties:
1
class WellKnownMediaFormats {
2
/// Native MediaFormat for adts/aac
3
static AdtsAacMediaFormat adtsAac = AdtsAacMediaFormat();
4
​
5
/// MediaFormat for caf/opus
6
static CafOpusMediaFormat cafOpus = CafOpusMediaFormat();
7
​
8
/// MediaFormat pcm
9
static PCMMediaFormat pcm = PCMMediaFormat();
10
​
11
/// MediaFormat ogg/opus
12
static OggOpusMediaFormat oggOpus = OggOpusMediaFormat();
13
​
14
/// Native MediaFormat ogg/vorbis
15
static OggVorbisMediaFormat oggVorbis = OggVorbisMediaFormat();
16
​
17
/// Native MediaFormat mp3
18
static MP3MediaFormat mp3 = MP3MediaFormat();
19
}
Copied!
To obtain a recording in a non-native MediaFormat, record the audio using one of the NativeMediaFormats and then use the sounds_codec package to trans-code the results into the desired MediaFormat.
The follow table details the navively support MediaFormat on each platform.
​
AAC
OGG/Opus
CAF/Opus
MP3
OGG/Vorbis
PCM
iOS
Yes
Yes
Yes
No
No
Yes
Android
Yes
No
No
No
No
No
Note: the supported formats does vary depending on the SDK Version. Call MediaFormat.isNativeEncoder on the desired MediaFormat to determine if the format is supported on the current OS/SDK version. AdtsAacMediaFormat is the safest as it is supported on both OS's and has been for some time.

Recording

To record you must specify the Track to record into using the Track.fromFile constructor. The Track file MUST exists and should be empty. When you start recording any existing content will be deleted.
Once you have finished recording you MUST release the SoundRecorder instance by calling SoundRecorder.release().
In the following example we create a temporary file to record into.
Once recording is stopped the onStopped method will be called.
We then play the recording to the user use QuickPlay and finally when QuickPlay completes we delete the temporary recording.
In the real world you probably want to save the recording rather than just deleting it.
1
var recording = Track.tempFile(WellKnownMediaFormats.adtsAac);
2
var track =
3
Track.fromFile(recording, mediaFormat: WellKnownMediaFormats.adtsAac);
4
var recorder = SoundRecorder();
5
recorder.onStopped = ({wasUser}) {
6
recorder.release();
7
​
8
/// recording has finished so play it back to the user.
9
QuickPlay.fromTrack(track, onStopped: (() {
10
/// delete the temp file now we are done with it.
11
File(recording).delete();
12
}));
13
};
14
recorder.record(track);
15
​
16
recorder.stop();
Copied!

Permissions

In order to record audio you need to request access to the devices microphone.
Sounds will NOT request permissions. It is your responsibility to ensure that the appropriate permissions have been granted.
If the appropriate permissions are not available a RecordingPermissionException will be thrown.
You might want to look at the package permission_handler to obtain the required permissions.
The SoundRecorder class however does provide method to delay the permission request until it is actually required. SoundRecorder provides a callback that is called just before recording starts. You can use this callback to prompt the user for the required permissions.
1
var recording = Track.tempFile(WellKnownMediaFormats.adtsAac);
2
var track =
3
Track.fromFile(recording, mediaFormat: WellKnownMediaFormats.adtsAac);
4
​
5
var recorder = SoundRecorder();
6
recorder.onRequestPermissions = (track) => askUserForPermission(track);
7
recorder.record(track);
Copied!

Recording quality

You can control the audio quality of a recording via the MediaFormat used when you create the Track you are going to record into.
1
var recording = Track.tempFile(WellKnownMediaFormats.adtsAac);
2
var track = Track.fromFile(recording,
3
mediaFormat: AdtsAacMediaFormat(
4
sampleRate: 16000,
5
numChannels: 1,
6
bitRate: 16000,
7
));
8
​
9
var recorder = SoundRecorder();
10
recorder.onStopped = ({bool wasUser}) {
11
recorder.release();
12
};
13
​
14
recorder.record(track, audioSource: AudioSource.mic);
Copied!
The above examples uses the default values so you can exclude any you don't need to change.

Audio Source

When recording you may want to choose the audio source.
The default audio source is the microphone. The set of supported sources is platform dependant.
TODO: provide a table of supported sources and what we do when an invalid source is passed.
You choose the audio source when calling SoundRecorder.record().
1
var recording = Track.tempFile(WellKnownMediaFormats.adtsAac);
2
var track =
3
Track.fromFile(recording, mediaFormat: WellKnownMediaFormats.adtsAac);
4
​
5
var recorder = SoundRecorder();
6
recorder.record(track, audioSource: AudioSource.mic);
Copied!
See the AudioSource class for details on the available sources.

Monitoring progress

Whilst recording you may want to monitor the progress of the recording. You can do this using by calling SoundRecorder.dispositionStream.
1
Stream<RecordingDisposition> stream = SoundRecorder()
2
.dispositionStream(interval: Duration(milliseconds: 100));
3
stream.listen((disposition) {});
Copied!
Typically you would consume the stream using a Flutter StreamBuilder but how you us it is up to you.
When subscribing to a SoundRecorder stream you may set an interval. By default you will receive an stream event every 10 milliseconds.
The RecordingDisposition object provided by the stream contains both the current duration of the recording and the current decibels (how load it is).
1
class RecordingDisposition {
2
final Duration duration;
3
final double decibels;
4
}
Copied!

Stopping a recording

To stop a recording you call SoundRecorder.stop().
1
var recorder = SoundRecorder();
2
recorder.record(Track.fromFile('path to file',
3
mediaFormat: WellKnownMediaFormats.adtsAac));
4
​
5
/// some widget event
6
void onTap() {
7
recorder.stop();
8
}
Copied!

Pause recorder

You can pause the recording at any point between calling start/stop.
On Android this API verb needs al least SDK-24.
1
recorder.pause();
Copied!
Resume recorder
If you pause the recording you need to resume the recording.
On Android this API verb needs at least SDK-24.
1
recorder.resume();
Copied!

cleanup

Create a recorder you must ensure that you call release() on the recorder instance. If you fail to call release then you make stop other applications (and your own app) from doing any more recording.
You MUST also ensure that the recorder has been stopped when your widget is detached from the ui. Overload your widget's dispose() method to stop the recorder when your widget is disposed.
1
@override
2
void dispose() {
3
recorder.release();
4
super.dispose();
5
}
Copied!
Last modified 7mo ago