flutter sound 2-in-1
2-in-1 sound recorder & player. a flutter plugin for sound.
this plugin provides simple 2-in-1 recorder and player functionalities for both android and ios platforms. this only supports the default file extension for each platform. this plugin handles file from remote url. this plugin can handle playback stream from native (to sync exact time with bridging).
install
add flutter_sound
as a dependency in pubspec.yaml
for help on adding as a dependency, view the documentation.
post installation
on ios you need to add a usage description to info.plist
:
<key>nsmicrophoneusagedescription</key>
<string>this sample uses the microphone to record your speech and convert it to text.</string>
<key>uibackgroundmodes</key>
<array>
<string>audio</string>
</array>
on android you need to add a permission to androidmanifest.xml
:
<uses-permission android:name="android.permission.record_audio" />
<uses-permission android:name="android.permission.write_external_storage" />
methods
func | param | return | description |
---|---|---|---|
setsubscriptionduration | double sec | string message | set subscription timer in seconds. default is 0.01 if not using this method. |
startrecorder | string uri | string uri | start recording. this will return uri used. |
stoprecorder | string message | stop recording. | |
startplayer | string uri | string message | start playing. |
stopplayer | string message | stop playing. | |
pauseplayer | string message | pause playing. | |
resumeplayer | string message | resume playing. | |
seektoplayer | int millisecs position to goto | string message | seek audio to selected position in seconds. parameter should be less than audio duration to correctly placed. |
subscriptions
subscription | return | description |
---|---|---|
onrecorderstatechanged | <recordstatus> | able to listen to subscription when recorder starts. |
onplayerstatechanged | <playstatus> | able to listen to subscription when player starts. |
default uri path
when uri path is not set during the function call
in startrecorder
or startplayer
, they are saved in below path depending on the platform.
- default path for android
sdcard/sound.mp4
.
- default path for ios
sound.m4a
.
usage
creating instance.
fluttersound fluttersound = new fluttersound();
starting recorder with listener.
string path = await fluttersound.startplayer(null);
print('startplayer: $path');
_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);
print('startplayer: $path');
_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();
print('stopplayer: $result');
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 subscription duration (optional)
/// 0.01 is default value when not set.
fluttersound.setsubscriptionduration(0.01);
setting volume.
/// 1.0 is default
/// currently, volume can be changed when player is running. try manage this right after player starts.
string path = await fluttersound.startplayer(null);
await fluttersound.setvolume(0.1);
todo
- [ ] seeking example in
exmaple
project - [x] volume control
- [x] sync timing for recorder callback handler
Comments are closed.