Crate rodio [] [src]

Usage

There are two main concepts in this library:

  • Sources, represented with the Source trait, that provide sound data.
  • Sinks, which accept sound data.

In order to play a sound, you need to create a source, a sink, and connect the two. For example here is how you play a sound file:

use std::io::BufReader;
 
let endpoint = rodio::get_default_endpoint().unwrap();
let sink = rodio::Sink::new(&endpoint);
 
let file = std::fs::File::open("music.ogg").unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
sink.append(source);Run

The append method takes ownership of the source and starts playing it. If a sink is already playing a sound when you call append, the sound is added to a queue and will start playing when the existing source is over.

If you want to play multiple sounds simultaneously, you should create multiple sinks.

How it works

Rodio spawns a background thread that is dedicated to reading from the sources and sending the output to the endpoint.

All the sounds are mixed together by rodio before being sent. Since this is handled by the software, there is no restriction for the number of sinks that can be created.

Adding effects

The Source trait provides various filters, similarly to the standard Iterator trait.

Example:

use rodio::Source;
use std::time::Duration;
 
// repeats the first five seconds of this sound forever
let source = source.take_duration(Duration::from_secs(5)).repeat_infinite();Run

Reexports

pub use decoder::Decoder;
pub use source::Source;

Modules

decoder
source

Structs

Endpoint

An opaque type that identifies an end point.

Sink

Handle to an endpoint that outputs sounds.

Traits

Sample

Represents a value of a single sample.

Functions

get_default_endpoint

Return the default endpoint, or None if no device is available.

get_endpoints_list

Return an iterator to the list of formats that are supported by the system.

play_once

Plays a sound once. Returns a Sink that can be used to control the sound.