A powerful Rust asynchronous audio playback library with support for local file playback and network streaming.
English | 简体中文
- 🎵 Multiple Format Support - Supports common audio formats including MP3, WAV, FLAC, OGG, and more
- 🌐 Network Streaming - Load and play audio streams from URLs
- ⚡ Async Loading - Tokio-based asynchronous downloading and playback
- 🎛️ Full Control - Play, pause, seek, volume control, and more
- 📡 Event-Driven - Rich event callback system for playback events
- 🔧 Flexible Extension - Support for custom Readers and Sources
Add the dependency to your Cargo.toml:
[dependencies]
remu-audio = "0.1.0"use remu_audio::player::{Player, PlaybackControl};
use remu_audio::events::PlayerEvent;
use anyhow::Result;
#[tokio::main]
async fn main() -> Result<()> {
// Create a player instance
let mut player = Player::new()?;
// Set up event callback
player.set_callback(|event| {
match event {
PlayerEvent::Play => println!("Started playing"),
PlayerEvent::Pause => println!("Paused"),
PlayerEvent::Ended => println!("Playback ended"),
_ => {}
}
});
// Load a local file
player.load_file("audio.mp3").await?;
// Start playback
player.play();
// Wait for playback
std::thread::sleep(std::time::Duration::from_secs(10));
Ok(())
}// Load audio from URL
player.load_url("https://example.com/audio.mp3").await?;
player.play();// Pause
player.pause();
// Resume playback
player.play();
// Seek to position (20 seconds)
player.seek(Duration::from_secs(20))?;
// Set volume (0.0 - 1.0)
player.set_volume(0.5);
// Get playback state
let is_paused = player.paused();
let position = player.position();
let duration = player.duration();
let volume = player.volume();The main player class providing audio loading and playback functionality.
new()- Create a new player instanceload_file(path: &str)- Load a local audio fileload_url(url: &str)- Load audio from a URLload_reader<R>(reader: R)- Load from a custom Readerload_source(source: impl Source)- Load from a Sourceset_callback<F>(callback: F)- Set playback event callbackset_loader_callback<F>(callback: F)- Set loader event callbackstop()- Stop playback and clear stateended()- Check if playback has ended
Provides playback control interface, implemented by Player and PlayerControl.
play()- Start/resume playbackpause()- Pause playbackseek(position: Duration)- Seek to a specific positionset_volume(volume: f32)- Set volume (0.0 - 1.0)paused()- Get pause stateposition()- Get current playback positionduration()- Get total durationvolume()- Get current volume
Player event enumeration used for event callbacks.
Play- Playback started or resumed from pausePause- Playback pausedPlaying- Currently playing (data sufficient)Waiting- Buffering/waiting for dataEnded- Playback endedEmptied- Playback content clearedDurationChange- Duration changedVolumeChange- Volume changedSeeking- Seek operation startedSeeked- Seek operation completedLoadStart- Loading startedLoadedData- Data loadedLoadedMetadata- Metadata loadedError { message: String }- An error occurred
Loader event enumeration for monitoring download status.
Completed- Download completedAborted- Download aborted
let mut player = Player::new()?;
// Set up comprehensive event listeners
player.set_callback(|event| {
match event {
PlayerEvent::LoadStart => {
println!("Loading...");
}
PlayerEvent::LoadedMetadata => {
println!("Ready to play");
}
PlayerEvent::Play => {
println!("▶️ Playing");
}
PlayerEvent::Pause => {
println!("⏸️ Paused");
}
PlayerEvent::Ended => {
println!("✅ Playback completed");
}
PlayerEvent::Error { message } => {
eprintln!("❌ Error: {}", message);
}
_ => {}
}
});
player.load_file("song.mp3").await?;
player.play();let mut player = Player::new()?;
// Monitor download progress
player.set_loader_callback(|event| {
match event {
LoaderEvent::Completed => {
println!("✅ Download completed");
}
LoaderEvent::Aborted => {
println!("⚠️ Download aborted");
}
}
});
// Load network audio
player.load_url("https://example.com/stream.mp3").await?;
player.play();let mut player = Player::new()?;
let control = player.control();
// Control playback from another thread
std::thread::spawn(move || {
let ctrl = control.read().unwrap();
ctrl.play();
std::thread::sleep(Duration::from_secs(5));
ctrl.pause();
});rodio- Audio playback coresymphonia- Audio decodingcpal- Cross-platform audio I/Otokio- Async runtimereqwest- HTTP clientanyhow- Error handling
The project includes complete example code demonstrating various use cases:
cargo run --example test_playbackThe example file is located at examples/test_playback.rs and includes:
- Local file playback
- Network URL playback
- Playback control (play, pause, seek)
- Volume adjustment
- Event listeners
cargo buildcargo testcargo run --example test_playbackThis project is licensed under the MIT License - see the LICENSE file for details.
Issues and Pull Requests are welcome!
- Project Homepage: https://github.com/Minteea/remu-audio
- Issue Tracker: https://github.com/Minteea/remu-audio/issues
Thanks to the following open source projects:
- rodio - Audio playback library
- symphonia - Audio decoding library
- cpal - Cross-platform audio library
✨ This README was generated with GitHub Copilot ✨