Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/kira/src/sound/streaming/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ impl StreamingSound {
}
}

impl Drop for StreamingSound {
fn drop(&mut self) {
// the decode scheduler's thread only ends once the sound is stopped or
// reaches its end. a sound can be dropped without either happening, for
// example when it's refused because the sound limit was reached, or when
// the audio manager is dropped while it's playing, so mark it stopped here
// to end the thread.
self.shared.set_state(PlaybackState::Stopped);
}
}

impl Sound for StreamingSound {
fn on_start_processing(&mut self) {
self.update_current_frame();
Expand Down
16 changes: 16 additions & 0 deletions crates/kira/src/sound/streaming/sound/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ fn plays_all_samples() {
);
}

/// Tests that dropping a `StreamingSound` that was never stopped ends its
/// decode scheduler, as happens when the sound limit refuses the sound or the
/// audio manager is dropped while it's playing.
#[test]
fn dropping_sound_ends_decode_scheduler() {
let data = StreamingSoundData {
decoder: Box::new(MockDecoder::new(vec![Frame::from_mono(1.0); 100])),
settings: StreamingSoundSettings::new(),
slice: None,
};
let (sound, _, mut scheduler) = data.split().unwrap();
assert!(matches!(scheduler.run().unwrap(), NextStep::Continue));
drop(sound);
assert!(matches!(scheduler.run().unwrap(), NextStep::End));
}

/// Tests that a `StreamingSound` will pause playback while waiting
/// for samples from the decoder.
#[test]
Expand Down
Loading