Struct DecoderBuilder

Source
pub struct DecoderBuilder<R> { /* private fields */ }
Expand description

Builder for configuring and creating a decoder.

This provides a flexible way to configure decoder settings before creating the actual decoder instance.

§Examples

use std::fs::File;
use rodio::decoder::DecoderBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file = File::open("audio.mp3")?;
    let decoder = DecoderBuilder::new()
        .with_data(file)
        .with_hint("mp3")
        .with_gapless(true)
        .build()?;

    // Use the decoder...
    Ok(())
}

Implementations§

Source§

impl<R: Read + Seek + Send + Sync + 'static> DecoderBuilder<R>

Source

pub fn new() -> Self

Creates a new decoder builder with default settings.

§Examples
use std::fs::File;
use rodio::decoder::DecoderBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file = File::open("audio.mp3")?;
    let decoder = DecoderBuilder::new()
        .with_data(file)
        .build()?;

    // Use the decoder...
    Ok(())
}
Source

pub fn with_data(self, data: R) -> Self

Sets the input data source to decode.

Source

pub fn with_byte_len(self, byte_len: u64) -> Self

Sets the byte length of the stream. This is required for:

  • Reliable seeking operations
  • Duration calculations in formats that lack timing information (e.g. MP3, Vorbis)

Note that this also sets is_seekable to true.

The byte length should typically be obtained from file metadata:

use std::fs::File;
use rodio::Decoder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file = File::open("audio.mp3")?;
    let len = file.metadata()?.len();
    let decoder = Decoder::builder()
        .with_data(file)
        .with_byte_len(len)
        .build()?;

    // Use the decoder...
    Ok(())
}

Alternatively, it can be obtained by seeking to the end of the stream.

An incorrect byte length can lead to unexpected behavior, including but not limited to incorrect duration calculations and seeking errors.

Source

pub fn with_coarse_seek(self, coarse_seek: bool) -> Self

Enables or disables coarse seeking. This is disabled by default.

This needs byte_len to be set. Coarse seeking is faster but less accurate: it may seek to a position slightly before or after the requested one, especially when the bitrate is variable.

Source

pub fn with_gapless(self, gapless: bool) -> Self

Enables or disables gapless playback. This is enabled by default.

When enabled, removes silence between tracks for formats that support it.

Source

pub fn with_hint(self, hint: &str) -> Self

Sets a format hint for the decoder.

When known, this can help the decoder to select the correct codec faster. Common values are “mp3”, “wav”, “flac”, “ogg”, etc.

Source

pub fn with_mime_type(self, mime_type: &str) -> Self

Sets a mime type hint for the decoder.

When known, this can help the decoder to select the correct demuxer faster. Common values are “audio/mpeg”, “audio/vnd.wav”, “audio/flac”, “audio/ogg”, etc.

Source

pub fn with_seekable(self, is_seekable: bool) -> Self

Configure whether the data supports random access seeking. Without this, only forward seeking may work.

For reliable seeking behavior, byte_len should be set either from file metadata or by seeking to the end of the stream. While seeking may work without byte_len for some formats, it is not guaranteed.

§Examples
use std::fs::File;
use rodio::Decoder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let file = File::open("audio.mp3")?;
    let len = file.metadata()?.len();

    // Recommended: Set both byte_len and seekable
    let decoder = Decoder::builder()
        .with_data(file)
        .with_byte_len(len)
        .with_seekable(true)
        .build()?;

    // Use the decoder...
    Ok(())
}
Source

pub fn build(self) -> Result<Decoder<R>, DecoderError>

Creates a new decoder with previously configured settings.

§Errors

Returns DecoderError::UnrecognizedFormat if the audio format could not be determined or is not supported.

Source

pub fn build_looped(self) -> Result<LoopedDecoder<R>, DecoderError>

Creates a new looped decoder with previously configured settings.

§Errors

Returns DecoderError::UnrecognizedFormat if the audio format could not be determined or is not supported.

Trait Implementations§

Source§

impl<R: Clone> Clone for DecoderBuilder<R>

Source§

fn clone(&self) -> DecoderBuilder<R>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R: Debug> Debug for DecoderBuilder<R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R> Default for DecoderBuilder<R>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<R> Freeze for DecoderBuilder<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for DecoderBuilder<R>
where R: RefUnwindSafe,

§

impl<R> Send for DecoderBuilder<R>
where R: Send,

§

impl<R> Sync for DecoderBuilder<R>
where R: Sync,

§

impl<R> Unpin for DecoderBuilder<R>
where R: Unpin,

§

impl<R> UnwindSafe for DecoderBuilder<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,