Getting started
Playback
The complete source of a runnable example is there
1. FlutterSoundPlayer instanciation
To play back something you must instanciate a player. Most of the time, you will need just one player, and you can place this instanciation in the variables initialisation of your class :
import 'package:flutter_sound/flutter_sound.dart';
...
FlutterSoundPlayer _mPlayer = FlutterSoundPlayer();
2. Open and close your player
Before calling startPlayer()
you must open the Player.
When you have finished with it, you must close it. A good places to put those verbs are in the procedures initState()
and dispose()
.
FlutterSoundPlayer? _mPlayer = FlutterSoundPlayer();
bool _mPlayerIsInited = false;
@override
void initState() {
// Be careful : openPlayer returns a Future.
// Do not access your FlutterSoundPlayer before the completion of the Future
super.initState();
_mPlayer!.openPlayer().then((value) {
setState(() {
_mPlayerIsInited = true;
});
});
}
@override
void dispose() {
stopPlayer();
// Be careful : you must `close` the audio session when you have finished with it.
_mPlayer!.closePlayer();
_mPlayer = null;
super.dispose();
}
3. Play your sound
To play a sound you call startPlayer(). To stop a sound you call stopPlayer()
const _exampleAudioFilePathMP3 = 'https://tau.canardoux.xyz/live/extract/05.mp3';
void play() async {
await _mPlayer!.startPlayer(
fromURI: _exampleAudioFilePathMP3,
codec: Codec.mp3,
whenFinished: () {
setState(() {});
});
setState(() {});
}
Future<void> stopPlayer() async {
if (_mPlayer != null) {
await _mPlayer!.stopPlayer();
}
}
Player States
Recording
The complete source of a runnable example is there
1. FlutterSoundRecorder instanciation
To record something you must instanciate a recorder. Most of the time, you will need just one recorder, and you can place this instanciation in the variables initialisation of your class :
FlutterSoundRecorder _myRecorder = FlutterSoundRecorder();
2. Open and close your recorder
Before calling startRecorder()
you must open your recorder.
When you have finished with it, you must close it. A good place to put those verbs is in the procedures initState()
and dispose()
.
@override
void initState() {
super.initState();
_mPlayer!.openPlayer().then((value) {
setState(() {
_mPlayerIsInited = true;
});
});
}
@override
void dispose() {
stopPlayer();
// Be careful : you must `close` the recorder when you have finished with it.
_mPlayer!.closePlayer();
_mPlayer = null;
super.dispose();
}
3. Record something
To record something you call startRecorder(). To stop the recorder you call stopRecorder()
Future<void> record() async {
await _myRecorder.startRecorder(
toFile: _mPath,
codec: Codec.aacADTS,
);
}
Future<void> stopRecorder() async {
await _myRecorder.stopRecorder();
}
Recorder States