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