flutter_sound 1.0.2 flutter_sound: ^1.0.2 copied to clipboard
Flutter plugin that relates to sound like audio and recorder.
flutter_sound #
This plugin provides simple recorder and player functionalities for both `android` and `ios` platforms. This only supports 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). [![Alt text for preview](https://firebasestorage.googleapis.com/v0/b/flutterdart-5d354.appspot.com/o/flutter_sound.gif?alt=media&token=f9e01ee6-0dc6-4988-b96a-52cc4f4824c4)Getting Started #
For help getting started with Flutter, view our online documentation.
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 sec 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. |
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(sec);
Setting subscription duration (Optional). 0.01 is default value when not set.
/// 0.01 is default
flutterSound.setSubscriptionDuration(0.01);