Skip to main content

Flutter_sound Examples

Flutter Sound: How To Examples

Flutter Sound is a Flutter plugin that provides audio recording and playback capabilities. In this tutorial, we will explore various examples to demonstrate how to use Flutter Sound for different audio-related tasks. We will cover installation, recording audio, playing audio, and more.

Installation

To use Flutter Sound in your Flutter project, add the following dependency to your pubspec.yaml file:

dependencies:
flutter_sound: ^3.0.0

Then run flutter pub get to fetch the dependency.

Example 1: Recording Audio

In this example, we will demonstrate how to record audio using Flutter Sound.

  1. Import the necessary packages:
import 'package:flutter_sound/flutter_sound.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
  1. Set up the recording logic:
FlutterSoundRecorder? _recorder;
String? _recordingPath;

Future<void> startRecording() async {
final appDir = await path_provider.getApplicationDocumentsDirectory();
final recordingPath = '${appDir.path}/recording.aac';

_recorder = FlutterSoundRecorder();
await _recorder!.openAudioSession();
await _recorder!.startRecorder(toFile: recordingPath);

setState(() {
_recordingPath = recordingPath;
});
}

Future<void> stopRecording() async {
await _recorder!.stopRecorder();
await _recorder!.closeAudioSession();

setState(() {
_recorder = null;
_recordingPath = null;
});
}
  1. Create UI elements to start and stop the recording:
ElevatedButton(
onPressed: _recorder == null ? startRecording : stopRecording,
child: Text(_recorder == null ? 'Start Recording' : 'Stop Recording'),
),

Example 2: Playing Audio

In this example, we will demonstrate how to play audio using Flutter Sound.

  1. Import the necessary packages:
import 'package:flutter_sound/flutter_sound.dart';
  1. Set up the audio playback logic:
FlutterSoundPlayer? _player;

Future<void> playAudio(String filePath) async {
_player = FlutterSoundPlayer();
await _player!.openAudioSession();
await _player!.startPlayer(fromURI: filePath);
}

Future<void> stopAudio() async {
await _player!.stopPlayer();
await _player!.closeAudioSession();

setState(() {
_player = null;
});
}
  1. Create UI elements to play and stop the audio:
ElevatedButton(
onPressed: () => playAudio('path/to/audio/file.aac'),
child: Text('Play Audio'),
),
ElevatedButton(
onPressed: stopAudio,
child: Text('Stop Audio'),
),

Example 3: Recording and Playing Audio

In this example, we will combine the previous examples to demonstrate recording and playing audio using Flutter Sound.

  1. Import the necessary packages:
import 'package:flutter_sound/flutter_sound.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
  1. Set up the recording and audio playback logic:
FlutterSoundRecorder? _recorder;
FlutterSoundPlayer? _player;
String? _recordingPath;

Future<void> startRecording() async {
final appDir = await path_provider.getApplicationDocumentsDirectory();
final recordingPath = '${appDir.path}/recording.aac';

_recorder = FlutterSoundRecorder();
await _recorder!.openAudioSession();
await _recorder!.startRecorder(toFile: recordingPath);

setState(() {
_recordingPath = recordingPath;
});
}

Future<void> stopRecording() async {
await _recorder!.stopRecorder();
await _recorder!.closeAudioSession();

setState(() {
_recorder = null;
_recordingPath = null;
});
}

Future<void> playAudio(String filePath) async {
_player = FlutterSoundPlayer();
await _player!.openAudioSession();
await _player!.startPlayer(fromURI: filePath);
}

Future<void> stopAudio() async {
await _player!.stopPlayer();
await _player!.closeAudioSession();

setState(() {
_player = null;
});
}
  1. Create UI elements to control recording and audio playback:
ElevatedButton(
onPressed: _recorder == null ? startRecording : stopRecording,
child: Text(_recorder == null ? 'Start Recording' : 'Stop Recording'),
),
ElevatedButton(
onPressed: () => playAudio(_recordingPath!),
child: Text('Play Recorded Audio'),
),
ElevatedButton(
onPressed: stopAudio,
child: Text('Stop Audio'),
),

These are just a few examples of what you can do with Flutter Sound. You can explore the complete documentation of the flutter_sound package for more features and options.

Remember to import the necessary packages and set up the required logic based on your specific audio-related tasks.