This guide will help you update your code when upgrading from older versions of rodio. While we did our best, we might have missed things. PRs that improve this guide are very welcome!
The list below only contains required code changes. For a complete list of changes and new features, see CHANGELOG.md.
No changes are required.
- Playback logic has been turned into a feature that is enabled by default.
If you have
default_features = falsein yourCargo.tomland want audio playback, you need to also setfeatures = ["playback"]. - The default decoders have changed to Symphonia, which itself is licensed
under MPL. If you want to revert to the old decoders, you need to set
default_features = falseand enable theclaxon,houndandlewtonfeatures inCargo.tomlfor respectively FLAC, WAV and Ogg Vorbis.
OutputStreamHandleno longer exists, you can remove it from your code.OutputStreamHandle::play_rawhas been removed, instead useOutputStream.mixer().add().- The output stream is now more configurable. Where you used
OutputStream::try_default(), you need to change to either:- (recommended)
OutputStreamBuilder::open_default_stream()?which tries to open a new output stream for the default output device with its default configuration. Failing that it attempt to open an output stream with alternative configuration and/or non default output devices. Returns the stream for the first configurations tried that succeeds. If all attempts fail returns the initial error. - (org behavior)
open_stream_or_fallback()?which is used as follows:That tries to opening a output stream with the default configuration on the default device. Failing that attempt to open a stream with other available configurations supported by the default device. If all attempts fail returns initial error.let default_device = cpal::default_host() .default_output_device() .ok_or("No default audio output device is found.")?; rodio::OutputStreamBuilder::from_device(default_device)? .open_stream_or_fallback()?;
- (recommended)
- The output stream now prints to stderr or logs a message on drop, if that breaks your
CLI/UI use
stream.log_on_drop(false).
- Replace
Sink::try_newwithSink::connect_new, which takes an&Mixerinstead of aOutputStreamHandle. You get an&Mixerby callingmixer()onOutputStream. - Replace
Sink::new_idlewithSink::new.
The following Rodio 0.20 code:
let (_stream, handle) = rodio::OutputStream::try_default()?;
let player = rodio::Player::try_new(&handle)?;Should be written like this in Rodio 0.21:
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
let player = rodio::Player::connect_new(stream_handle.mixer());The SpatialSink changes mirror those in Sink described above.
Decoder::new_mp4no longer takes anMp4Typeas hint. Remove the hint.- The Symphonia decoders no longer assumes all sources are seekable. Use
DecoderBuilder::with_seekableortry_fromon aFile. You do not need to wrap it into aBufReaderanymore.
The following Rodio 0.20 code
let file = File::open("music.ogg")?;
let reader = BufReader::new(file);
let source = Decoder::new(reader);Should be written like this in Rodio 0.21:
let file = File::open("music.ogg")?;
let source = Decoder::try_from(file)?;- Replace
DynamicMixerControllerwithMixerandDynamicMixerwithMixerSource.
- The
Source::whiteandSource::pinkmethods have been deprecated. UseWhiteUniform::newandPink::newinstead.
- The
Sourcetrait had a required methodcurrent_frame_len, which has been renamed tocurrent_span_len. Rename every occurrence. Sourcewas generic over sample typesf32,u16, andi16. It no longer is; rodio now works withf32everywhere. This means:- Remove any generics (
::<f32>,::<u16>or::<i16>) that cause errors. - Remove any use of
SampleConvertor. - Remove any calls to
convert_samples.
- Remove any generics (