ez_audi/cpal_abstraction/
stream.rs

1use cpal;
2use cpal::traits::StreamTrait;
3
4use crate::errors::Error;
5
6/// An audio stream, stops the stream when dropped
7pub struct Stream {
8    stream: cpal::Stream
9}
10
11impl Stream {
12    fn new(stream: cpal::Stream) -> Stream {
13        Stream {
14            stream,
15        }
16    }
17
18    /// Continues/Starts the audio from where it ended
19    pub fn start(&self) -> Error<()> {
20        match self.stream.play() {
21            Ok(_) => Ok(()),
22            Err(e) => Err(e.into())
23        }
24    }
25
26    /// Stops the audio until it is explicitly continued
27    pub fn stop(&self) -> Error<()> {
28        match self.stream.pause() {
29            Ok(_) => Ok(()),
30            Err(e) => Err(e.into())
31        }
32    }
33}
34
35impl From<cpal::Stream> for Stream {
36    fn from(value: cpal::Stream) -> Self {
37        Stream::new(value)
38    }
39}