From 069faf762d4a238cfd2f9804e707ecbd707cf642 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Thu, 22 Oct 2015 16:45:33 +0200 Subject: [PATCH] Add a DecoderError and publish 0.3 --- Cargo.toml | 2 +- examples/basic.rs | 6 +++--- examples/music_ogg.rs | 2 +- examples/music_wav.rs | 2 +- src/decoder/mod.rs | 15 +++++++++++---- src/lib.rs | 8 ++++---- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 27cd384..c01dce2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rodio" -version = "0.2.2" +version = "0.3.0" authors = ["Pierre Krieger "] license = "Apache-2.0" description = "Audio playback library" diff --git a/examples/basic.rs b/examples/basic.rs index dbfc01e..9320178 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -6,17 +6,17 @@ fn main() { let endpoint = rodio::get_default_endpoint().unwrap(); let file = std::fs::File::open("examples/beep.wav").unwrap(); - let mut beep1 = rodio::play_once(&endpoint, BufReader::new(file)); + let mut beep1 = rodio::play_once(&endpoint, BufReader::new(file)).unwrap(); beep1.set_volume(0.2); std::thread::sleep_ms(1000); let file = std::fs::File::open("examples/beep2.wav").unwrap(); - rodio::play_once(&endpoint, BufReader::new(file)).detach(); + rodio::play_once(&endpoint, BufReader::new(file)).unwrap().detach(); std::thread::sleep_ms(1000); let file = std::fs::File::open("examples/beep3.ogg").unwrap(); - let beep3 = rodio::play_once(&endpoint, file); + let beep3 = rodio::play_once(&endpoint, file).unwrap(); std::thread::sleep_ms(1000); drop(beep1); diff --git a/examples/music_ogg.rs b/examples/music_ogg.rs index 7c6e46e..6cdddc9 100644 --- a/examples/music_ogg.rs +++ b/examples/music_ogg.rs @@ -7,7 +7,7 @@ fn main() { let sink = rodio::Sink::new(&endpoint); let file = std::fs::File::open("examples/music.ogg").unwrap(); - sink.append(rodio::Decoder::new(BufReader::new(file))); + sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap()); std::thread::sleep_ms(60000); } diff --git a/examples/music_wav.rs b/examples/music_wav.rs index e6dd57b..a31d424 100644 --- a/examples/music_wav.rs +++ b/examples/music_wav.rs @@ -7,7 +7,7 @@ fn main() { let sink = rodio::Sink::new(&endpoint); let file = std::fs::File::open("examples/music.wav").unwrap(); - sink.append(rodio::Decoder::new(BufReader::new(file))); + sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap()); sink.sleep_until_end(); } diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index a109bb6..7e314ae 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -18,19 +18,19 @@ enum DecoderImpl where R: Read + Seek { } impl Decoder where R: Read + Seek + Send + 'static { - pub fn new(data: R) -> Decoder { + pub fn new(data: R) -> Result, DecoderError> { let data = match wav::WavDecoder::new(data) { Err(data) => data, Ok(decoder) => { - return Decoder(DecoderImpl::Wav(decoder)); + return Ok(Decoder(DecoderImpl::Wav(decoder))); } }; if let Ok(decoder) = vorbis::VorbisDecoder::new(data) { - return Decoder(DecoderImpl::Vorbis(decoder)); + return Ok(Decoder(DecoderImpl::Vorbis(decoder))); } - panic!("Invalid format"); + Err(DecoderError::UnrecognizedFormat) } } @@ -87,3 +87,10 @@ impl Source for Decoder where R: Read + Seek { } } } + +/// Error that can happen when creating a decoder. +#[derive(Debug, Clone)] +pub enum DecoderError { + /// The format of the data has not been recognized. + UnrecognizedFormat, +} diff --git a/src/lib.rs b/src/lib.rs index 02772c2..31c3663 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,7 @@ //! let sink = rodio::Sink::new(&endpoint); //! //! let file = std::fs::File::open("music.ogg").unwrap(); -//! let source = rodio::Decoder::new(BufReader::new(file)); +//! let source = rodio::Decoder::new(BufReader::new(file)).unwrap(); //! sink.append(source); //! ``` //! @@ -147,11 +147,11 @@ impl Drop for Sink { /// Plays a sound once. Returns a `Sink` that can be used to control the sound. #[inline] -pub fn play_once(endpoint: &Endpoint, input: R) -> Sink +pub fn play_once(endpoint: &Endpoint, input: R) -> Result where R: Read + Seek + Send + 'static { - let input = decoder::Decoder::new(input); + let input = try!(decoder::Decoder::new(input)); let sink = Sink::new(endpoint); sink.append(input); - sink + Ok(sink) }