Add a DecoderError and publish 0.3

This commit is contained in:
Pierre Krieger 2015-10-22 16:45:33 +02:00
parent 642d025e02
commit 069faf762d
6 changed files with 21 additions and 14 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "rodio" name = "rodio"
version = "0.2.2" version = "0.3.0"
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"] authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"]
license = "Apache-2.0" license = "Apache-2.0"
description = "Audio playback library" description = "Audio playback library"

View file

@ -6,17 +6,17 @@ fn main() {
let endpoint = rodio::get_default_endpoint().unwrap(); let endpoint = rodio::get_default_endpoint().unwrap();
let file = std::fs::File::open("examples/beep.wav").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); beep1.set_volume(0.2);
std::thread::sleep_ms(1000); std::thread::sleep_ms(1000);
let file = std::fs::File::open("examples/beep2.wav").unwrap(); 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); std::thread::sleep_ms(1000);
let file = std::fs::File::open("examples/beep3.ogg").unwrap(); 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); std::thread::sleep_ms(1000);
drop(beep1); drop(beep1);

View file

@ -7,7 +7,7 @@ fn main() {
let sink = rodio::Sink::new(&endpoint); let sink = rodio::Sink::new(&endpoint);
let file = std::fs::File::open("examples/music.ogg").unwrap(); 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); std::thread::sleep_ms(60000);
} }

View file

@ -7,7 +7,7 @@ fn main() {
let sink = rodio::Sink::new(&endpoint); let sink = rodio::Sink::new(&endpoint);
let file = std::fs::File::open("examples/music.wav").unwrap(); 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(); sink.sleep_until_end();
} }

View file

@ -18,19 +18,19 @@ enum DecoderImpl<R> where R: Read + Seek {
} }
impl<R> Decoder<R> where R: Read + Seek + Send + 'static { impl<R> Decoder<R> where R: Read + Seek + Send + 'static {
pub fn new(data: R) -> Decoder<R> { pub fn new(data: R) -> Result<Decoder<R>, DecoderError> {
let data = match wav::WavDecoder::new(data) { let data = match wav::WavDecoder::new(data) {
Err(data) => data, Err(data) => data,
Ok(decoder) => { Ok(decoder) => {
return Decoder(DecoderImpl::Wav(decoder)); return Ok(Decoder(DecoderImpl::Wav(decoder)));
} }
}; };
if let Ok(decoder) = vorbis::VorbisDecoder::new(data) { 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<R> Source for Decoder<R> 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,
}

View file

@ -15,7 +15,7 @@
//! let sink = rodio::Sink::new(&endpoint); //! let sink = rodio::Sink::new(&endpoint);
//! //!
//! let file = std::fs::File::open("music.ogg").unwrap(); //! 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); //! 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. /// Plays a sound once. Returns a `Sink` that can be used to control the sound.
#[inline] #[inline]
pub fn play_once<R>(endpoint: &Endpoint, input: R) -> Sink pub fn play_once<R>(endpoint: &Endpoint, input: R) -> Result<Sink, decoder::DecoderError>
where R: Read + Seek + Send + 'static where R: Read + Seek + Send + 'static
{ {
let input = decoder::Decoder::new(input); let input = try!(decoder::Decoder::new(input));
let sink = Sink::new(endpoint); let sink = Sink::new(endpoint);
sink.append(input); sink.append(input);
sink Ok(sink)
} }