Flutter Sound Recorder and Player Example

You are currently viewing Flutter Sound Recorder and Player Example
Flutter Sound

Flutter Sound Recorder and Player Example

Flutter sound plugin for audio recording and audio player.

Flutter Sound
Flutter Sound

This plugin provides an audio recorder and audio player functionality for iOS and Android. This plugin also handles file from remote url.

Example

Flutter Sound
Flutter Sound

Install

Add flutter_sound as a dependency in pubspec.yaml

iOS

Add below usage in 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>

Android

Add permission in AndroidManifest.xml

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Methods

FuncParamReturnDescription
setSubscriptionDurationdouble secString messageSet subscription timer in seconds. Default is 0.01 if not using this method.
startRecorderString uriString uriStart recording. This will return uri used.
stopRecorderString messageStop recording.
startPlayerString uriString messageStart playing.
stopPlayerString messageStop playing.
pausePlayerString messagePause playing.
resumePlayerString messageResume playing.
seekToPlayerint milliSecs position to goToString messageSeek audio to selected position in seconds. Parameter should be less than audio duration to correctly placed.

Usage

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.

/// 0.01 is default
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);

Credit

This project is developed by Canardoux
Download this project from the below link.
https://github.com/Canardoux/flutter_sound/archive/refs/heads/master.zip
You can visit original source page from the below link:
https://github.com/Canardoux/flutter_sound

Spread the love
See also  Wraps around your existing audio allow it to run in the background with flutter

Leave a Reply