flutter_f2f_sound 1.0.1
flutter_f2f_sound: ^1.0.1 copied to clipboard
A powerful cross-platform audio plugin for Flutter. Supports audio playback, recording, system audio capture, and real-time streaming across Android, iOS, macOS, Windows, and Linux.
flutter_f2f_sound #
A powerful cross-platform audio plugin for Flutter that provides comprehensive audio playback, recording, and streaming capabilities across multiple platforms.
Features #
- đľ Audio Playback - Play audio from local files or network URLs (MP3, WAV, OGG, FLAC, AAC, M4A)
- đď¸ Audio Recording - Record audio from microphone with real-time streaming (Android, macOS, Windows, Linux)
- đĽď¸ System Audio Capture - Capture system audio output (Windows/Linux)
- đď¸ Volume Control - Real-time volume adjustment (0.0 to 1.0)
- âŻď¸ Playback Controls - Play, pause, stop, and resume audio
- đ Loop Playback - Support for looping audio playback
- đ Seeking Support - Seek to specific positions (Linux)
- đď¸ Format Conversion - Automatic sample rate conversion across all platforms
- đ Playback Information - Get current position and duration of audio
- đ Network Streaming - Non-blocking async playback from network URLs (all platforms)
- đĄ Real-time Audio Streams - Stream audio data to Flutter layer for processing
- đą Cross-Platform - Android, iOS, macOS, Windows, Linux
Platform Support #
| Platform | Playback | Recording | System Audio | Playback Stream | Recording Stream | Format Support | Audio Engine |
|---|---|---|---|---|---|---|---|
| Android | â | â | â | â | â | MP3, WAV, AAC, FLAC, OGG, M4A | MediaPlayer |
| iOS | â | â | â | â | â | MP3, WAV, AAC, ALAC, M4A | AVFoundation + AudioToolbox |
| Windows | â | â | â | â | â | MP3, WAV, WMA | Media Foundation + WASAPI |
| macOS | â | â | â | â | â | MP3, WAV, AAC, M4A | AVFoundation |
| Linux | â | â | â | â | â | MP3, WAV, OGG, FLAC, OPUS, VOC | PulseAudio + libsndfile |
Legend:
- Playback: Play local/network audio files
- Recording: Record audio from microphone
- System Audio: Capture system output audio (Windows/Linux only)
- Playback Stream: Stream audio data while playing to Flutter layer (macOS only)
- Recording Stream: Stream microphone audio data to Flutter layer
Notes:
- iOS supports microphone recording with real-time streaming
- macOS has full recording support and unique playback streaming capability
- System audio capture requires special permissions on Windows/Linux
Installation #
Add flutter_f2f_sound to your pubspec.yaml file:
dependencies:
flutter_f2f_sound:
path: ./flutter_f2f_sound
Or install from pub.dev (when published):
dependencies:
flutter_f2f_sound: ^1.0.0
Then run:
flutter pub get
Platform-Specific Setup #
Android #
No special setup required. The plugin uses MediaPlayer which is built into Android.
Permissions (optional, for network audio):
<uses-permission android:name="android.permission.INTERNET" />
iOS #
No special setup required. The plugin uses AVFoundation and AudioToolbox which are built into iOS.
Permissions (add to ios/Runner/Info.plist):
<key>NSMicrophoneUsageDescription</key>
<string>This app needs access to the microphone for recording audio.</string>
Windows #
No special setup required. The plugin uses Media Foundation and WASAPI which are built into Windows.
Linux #
Required System Libraries:
# Ubuntu/Debian
sudo apt-get install libpulse-dev libcurl4-openssl-dev libsndfile1-dev libsamplerate0-dev
# Fedora/RHEL
sudo dnf install pulseaudio-devel libcurl-devel libsndfile-devel libsamplerate-devel
# Arch Linux
sudo pacman -S pulseaudio libsndfile curl samplerate
Usage #
Import the package in your Dart code:
import 'package:flutter_f2f_sound/flutter_f2f_sound.dart';
Create an instance of the plugin:
final f2fSound = FlutterF2fSound();
Basic Audio Playback #
// Play audio from a local file
await f2fSound.play(
path: '/path/to/audio/file.mp3',
volume: 1.0,
loop: false,
);
// Play audio from a network URL (non-blocking)
await f2fSound.play(
path: 'https://example.com/audio/file.mp3',
volume: 0.8,
loop: true,
);
// Pause audio
await f2fSound.pause();
// Resume audio
await f2fSound.resume();
// Stop audio
await f2fSound.stop();
// Set volume (0.0 to 1.0)
await f2fSound.setVolume(0.5);
// Check if audio is playing
bool isPlaying = await f2fSound.isPlaying();
// Get current position in seconds
double position = await f2fSound.getCurrentPosition();
// Get duration of audio file in seconds
double duration = await f2fSound.getDuration('/path/to/audio/file.mp3');
Audio Recording #
// Start recording and get audio stream
final recordingStream = f2fSound.startRecording();
final subscription = recordingStream.listen(
(audioData) {
// audioData contains raw PCM samples (List<int>)
print('Received ${audioData.length} bytes of audio data');
// Process or save audio data as needed
},
);
// Stop recording
await f2fSound.stopRecording();
subscription.cancel();
System Audio Capture (Windows/Linux) #
// Start capturing system audio output
final systemSoundStream = f2fSound.startSystemSoundCapture();
final subscription = systemSoundStream.listen(
(audioData) {
// audioData contains raw PCM samples from system audio
print('Received ${audioData.length} bytes of system audio');
// Process or save system audio as needed
},
);
// Stop capturing (automatically stops when cancelled)
subscription.cancel();
await f2fSound.stopRecording();
Audio Playback Streaming #
// Stream audio data while playing (for processing)
final playbackStream = f2fSound.startPlaybackStream('/path/to/audio.wav');
final subscription = playbackStream.listen(
(audioData) {
// audioData contains raw PCM samples (16-bit)
print('Received ${audioData.length} bytes');
},
);
API Reference #
Methods #
Future<String?> getPlatformVersion()
Get the platform version string.
Future<void> play({required String path, double volume = 1.0, bool loop = false})
Play audio from the given path.
Parameters:
path: The path to the audio file (local file path or network URL)volume: The volume level (0.0 to 1.0, default: 1.0)loop: Whether to loop the audio playback (default: false)
Supported Path Formats:
- Local files:
/path/to/audio.mp3(Linux/Android),C:\path\to\audio.mp3(Windows) - Network URLs:
https://example.com/audio.mp3,http://example.com/audio.wav
Future<void> pause()
Pause the currently playing audio.
Future<void> resume()
Resume playback of paused audio.
Future<void> stop()
Stop the currently playing audio.
Future<void> setVolume(double volume)
Set the volume of the currently playing audio.
Parameters:
volume: The volume level (0.0 to 1.0)
Future<bool> isPlaying()
Check if audio is currently playing.
Returns: true if audio is playing, false otherwise
Future<double> getCurrentPosition()
Get the current playback position in seconds.
Returns: Current position in seconds (0.0 to duration)
Future<double> getDuration(String path)
Get the duration of the audio file in seconds.
Parameters:
path: The path to the audio file (local file path or network URL)
Returns: Duration in seconds, or 0.0 for network URLs (not immediately available)
Stream<List<int>> startRecording()
Start audio recording and get a stream of recorded audio data.
Returns: Stream of audio data as List<int> (PCM samples)
Future<void> stopRecording()
Stop audio recording.
Stream<List<int>> startPlaybackStream(String path)
Start audio playback and get a stream of playback audio data.
Parameters:
path: The path to the audio file
Returns: Stream of audio data as List<int> (PCM samples)
Stream<List<int>> startSystemSoundCapture()
Start system sound capture and get a stream of captured audio data.
Returns: Stream of audio data as List<int> (PCM samples)
Note: Only available on Windows and Linux
Technical Details #
Audio Format Support #
Platform-specific capabilities:
| Platform | MP3 | WAV | OGG | FLAC | AAC | M4A | Network | Sample Rate Conversion |
|---|---|---|---|---|---|---|---|---|
| Android | â | â | â | â | â | â | â | â (Automatic via MediaPlayer) |
| iOS | â | â | â | â | â | â | â | â (Automatic via AVFoundation) |
| Windows | â | â | â | â | â | â | â | â (Custom linear interpolation) |
| macOS | â | â | â | â | â | â | â | â (Automatic via AVFoundation) |
| Linux | â | â | â | â | â | â | â | â (libsamplerate, SINC_BEST_QUALITY) |
Notes:
- OGG/FLAC: Supported on Android/iOS/macOS through system codecs, on Linux via libsndfile
- M4A: Container format, supported on Android/iOS/macOS
- Network: All platforms support non-blocking async playback from HTTP/HTTPS URLs
Sample Rate Conversion #
All platforms support automatic sample rate conversion:
- Android/iOS: Built-in conversion via MediaPlayer/AVFoundation
- Windows: Custom linear interpolation resampling
- Linux: Professional quality conversion using libsamplerate (SRC_SINC_BEST_QUALITY)
Async/Non-Blocking Behavior #
All platforms implement non-blocking network audio playback:
- Android: Uses
prepareAsync()for MediaPlayer - iOS: Uses
AVPlayerfor network streams (non-blocking) - macOS: Uses
AVPlayerfor network streams (non-blocking) - Windows: Downloads on background thread
- Linux: Downloads via libcurl in background thread
Architecture #
Android #
- Uses
MediaPlayerfor playback - Uses
AudioRecordfor recording - Uses
AudioTrackfor streaming prepareAsync()ensures non-blocking network playback
iOS #
- Dual-player system:
AVPlayerfor network URLs (non-blocking streaming)AVAudioPlayerfor local files (fast loading)
- Automatic sample rate conversion via AVFoundation
AudioQueuefor real-time recording and streaming
macOS #
AVAudioPlayerandAVAudioEnginefor playbackAVAudioRecorderfor recordingAVAudioEnginefor playback streaming (unique to macOS)- Automatic sample rate conversion via AVFoundation
- Full recording and streaming support
Windows #
Media Foundationfor MP3/audio decodingWASAPI(Windows Audio Session API) for low-latency audio- Background thread download with WinHTTP
ISimpleAudioVolumefor volume control- Loopback recording for system audio capture
Linux #
PulseAudiofor audio I/Olibcurlfor network downloadslibsndfilefor audio format support (MP3, OGG, FLAC, etc.)libsampleratefor professional sample rate conversion- PulseAudio monitor stream for system audio capture
Performance Considerations #
- Network URLs: All platforms use non-blocking async playback
- Local Files: Fast loading with minimal latency
- Memory: Efficient streaming with configurable buffer sizes
- CPU: Optimized for real-time audio processing
Troubleshooting #
Audio not playing #
- Check file path: Ensure the path is correct and accessible
- File format: Verify the format is supported on your platform
- Network URLs: Test internet connection and URL accessibility
- Permissions: Check app has necessary permissions (especially on iOS/Android)
Volume control not working #
- Ensure volume is set between 0.0 and 1.0
- Check if audio is actually playing when setting volume
- On Windows, system volume may override app volume
System audio capture not working #
- Windows Only: Requires WASAPI loopback support (Windows 7+)
- Linux Only: Requires PulseAudio with monitor source
- Check system audio is actually playing (silent streams won't capture data)
Duration returns 0.0 for network URLs #
This is expected behavior. For network audio:
- Duration becomes available after buffering completes
- Use polling with exponential backoff to retrieve duration
- Example: Poll every 500ms with increasing delays
Example App #
A complete example app is included in the example directory demonstrating:
- Audio playback with controls
- Real-time position tracking
- Volume control
- Audio recording
- System audio capture (Windows/Linux)
- Network URL streaming
Run the example:
cd flutter_f2f_sound/example
flutter run
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License #
MIT License - see the LICENSE file for details.
Support #
If you find this plugin useful, please consider supporting its development:
Buy Me a Coffee â #
Your support helps maintain and improve this plugin. Any amount is appreciated!
垎俥ćŤç ćŻć
Changelog #
Version 1.0.0 #
- â Initial release
- â Cross-platform audio playback (Android, iOS, Windows, Linux)
- â Audio recording support (Android, iOS, Windows, Linux)
- â System audio capture (Windows, Linux)
- â Network streaming with non-blocking async
- â Automatic sample rate conversion
- â Volume and playback controls
- â Loop playback support
- â Real-time audio streaming
- â Multiple format support (MP3, WAV, OGG, FLAC, etc.)
Acknowledgments #
- Android: MediaPlayer API
- iOS: AVFoundation framework
- Windows: Media Foundation and WASAPI
- Linux: PulseAudio, libcurl, libsndfile, libsamplerate
Made with â¤ď¸ by the Flutter community