SampleStream also holds on to the AudioUnit so it is not dropped

This commit is contained in:
shockham 2017-04-19 11:44:42 +01:00
parent 8cfe176e52
commit 97657fae4a
1 changed files with 10 additions and 4 deletions

View File

@ -93,12 +93,13 @@ impl<T> Buffer<T> where T: Sample {
pub struct Voice { pub struct Voice {
playing: bool, playing: bool,
audio_unit: AudioUnit audio_unit: Arc<Mutex<AudioUnit>>,
} }
#[allow(dead_code)] // the audio_unit will be dropped if we don't hold it. #[allow(dead_code)] // the audio_unit will be dropped if we don't hold it.
pub struct SamplesStream { pub struct SamplesStream {
inner: Arc<Mutex<SamplesStreamInner>>, inner: Arc<Mutex<SamplesStreamInner>>,
audio_unit: Arc<Mutex<AudioUnit>>,
} }
@ -196,20 +197,24 @@ impl Voice {
try!(audio_unit.start().map_err(convert_error)); try!(audio_unit.start().map_err(convert_error));
let au_arc = Arc::new(Mutex::new(audio_unit));
let samples_stream = SamplesStream { let samples_stream = SamplesStream {
inner: inner, inner: inner,
audio_unit: au_arc.clone(),
}; };
Ok((Voice { Ok((Voice {
playing: true, playing: true,
audio_unit: audio_unit audio_unit: au_arc.clone(),
}, samples_stream)) }, samples_stream))
} }
#[inline] #[inline]
pub fn play(&mut self) { pub fn play(&mut self) {
if !self.playing { if !self.playing {
self.audio_unit.start().unwrap(); let mut unit = self.audio_unit.lock().unwrap();
unit.start().unwrap();
self.playing = true; self.playing = true;
} }
} }
@ -217,7 +222,8 @@ impl Voice {
#[inline] #[inline]
pub fn pause(&mut self) { pub fn pause(&mut self) {
if self.playing { if self.playing {
self.audio_unit.stop().unwrap(); let mut unit = self.audio_unit.lock().unwrap();
unit.stop().unwrap();
self.playing = false; self.playing = false;
} }
} }