mirror of
https://github.com/RustAudio/rodio
synced 2024-12-12 21:22:36 +00:00
Add a DecoderError and publish 0.3
This commit is contained in:
parent
642d025e02
commit
069faf762d
6 changed files with 21 additions and 14 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "rodio"
|
||||
version = "0.2.2"
|
||||
version = "0.3.0"
|
||||
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>"]
|
||||
license = "Apache-2.0"
|
||||
description = "Audio playback library"
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -18,19 +18,19 @@ enum DecoderImpl<R> where R: Read + Seek {
|
|||
}
|
||||
|
||||
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) {
|
||||
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<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,
|
||||
}
|
||||
|
|
|
@ -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<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
|
||||
{
|
||||
let input = decoder::Decoder::new(input);
|
||||
let input = try!(decoder::Decoder::new(input));
|
||||
let sink = Sink::new(endpoint);
|
||||
sink.append(input);
|
||||
sink
|
||||
Ok(sink)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue