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>
impl<R: Read + Seek + Send + Sync + 'static> DecoderBuilder<R>
Sourcepub fn new() -> Self
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(())
}
Sourcepub fn with_byte_len(self, byte_len: u64) -> Self
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.
Sourcepub fn with_coarse_seek(self, coarse_seek: bool) -> Self
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.
Sourcepub fn with_gapless(self, gapless: bool) -> Self
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.
Sourcepub fn with_hint(self, hint: &str) -> Self
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.
Sourcepub fn with_mime_type(self, mime_type: &str) -> Self
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.
Sourcepub fn with_seekable(self, is_seekable: bool) -> Self
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(())
}
Sourcepub fn build(self) -> Result<Decoder<R>, DecoderError>
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.
Sourcepub fn build_looped(self) -> Result<LoopedDecoder<R>, DecoderError>
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>
impl<R: Clone> Clone for DecoderBuilder<R>
Source§fn clone(&self) -> DecoderBuilder<R>
fn clone(&self) -> DecoderBuilder<R>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more