| Crates.io | rodio_playback_position |
| lib.rs | rodio_playback_position |
| version | 0.1.2 |
| created_at | 2025-08-04 15:04:22.655037+00 |
| updated_at | 2025-08-04 16:00:48.212485+00 |
| description | An alternative playback backend for rodio sources with accurate position tracking. |
| homepage | |
| repository | https://github.com/linobigatti/coop-editor/tree/master/rodio_playback_position |
| max_upload_size | |
| id | 1780821 |
| size | 50,875 |
A crate that provides a small playback backend for rodio sources, with support for a high-precision interpolated playback position counter.
This is useful for applications that need to sync visuals or other events with audio playback at runtime, such as rhythm games or music players. For scheduling and synchronizing the playback of multiple sources simultaneously, see the rodio_scheduler crate.
Add this to your Cargo.toml:
[dependencies]
rodio_playback_position = "0.1.2"
rodio = "0.21.1"
cpal = "0.16.0"
Here is an example of how to use the library to start playback and keep track of the current playback position. For more information, see the Docs, as well as the cpal Docs.
use std::time::Duration;
use cpal::traits::{HostTrait, DeviceTrait};
use rodio::source::SineWave;
use rodio::Source;
use rodio_playback_position::{OutputStreamConfig, stream};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Get a cpal output device.
let host = cpal::default_host();
let device = host.default_output_device().expect("no output device available");
println!("Using output device: {}", device.name()?);
// Create a config for the stream.
let config = OutputStreamConfig::from(device.default_output_config().unwrap());
// Create a source.
let source = SineWave::new(440.0).take_duration(Duration::from_secs(5));
// Create a stream handle.
println!("Opening stream...");
let mut stream_handle = stream::open(
&device,
&config,
source,
|err| eprintln!("stream error: {}", err),
)?;
println!("Stream opened.");
// Get the sample count.
for _ in 0..50 {
println!("Sample count: {}", stream_handle.sample_count());
std::thread::sleep(Duration::from_millis(100));
}
Ok(())
}