rodio/examples/seek_mp3.rs
2024-11-20 00:58:05 +04:00

25 lines
773 B
Rust

use std::error::Error;
use std::io::BufReader;
use std::time::Duration;
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());
let file = std::fs::File::open("assets/music.mp3")?;
sink.append(rodio::Decoder::new(BufReader::new(file))?);
std::thread::sleep(std::time::Duration::from_secs(2));
sink.try_seek(Duration::from_secs(0))?;
std::thread::sleep(std::time::Duration::from_secs(2));
sink.try_seek(Duration::from_secs(4))?;
sink.sleep_until_end();
// This doesn't do anything since the sound has ended already.
sink.try_seek(Duration::from_secs(5))?;
println!("seek example ended");
Ok(())
}