Skip to content

Commit

Permalink
Fix stopped playback state on channels (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Jul 18, 2022
1 parent 892d15b commit 63d4f82
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
6 changes: 3 additions & 3 deletions examples/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ fn start_audio(mut commands: Commands, asset_server: Res<AssetServer>, audio: Re
fn process_keyboard_input(audio: Res<Audio>, kb: Res<Input<KeyCode>>) {
if kb.just_pressed(KeyCode::P) {
audio.pause();
println!("Audio paused.");
println!("Audio pausing...");
} else if kb.just_pressed(KeyCode::S) {
audio.stop();
println!("Audio stopped.");
println!("Audio stopping...");
} else if kb.just_pressed(KeyCode::R) {
audio.resume();
println!("Audio resumed.");
println!("Audio resuming...");
}
}

Expand Down
21 changes: 20 additions & 1 deletion src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::audio_output::{play_audio_channel, update_instance_states};
use crate::audio_output::{play_audio_channel, update_instance_states, InstanceState};
use crate::source::AudioSource;
use crate::{AudioSystemLabel, ParallelSystemDescriptorCoercion};
use bevy::app::{App, CoreStage};
Expand Down Expand Up @@ -98,6 +98,25 @@ impl PlaybackState {
}
}

impl From<&InstanceState> for PlaybackState {
fn from(state: &InstanceState) -> Self {
let position = state.kira.position();
match state.kira.state() {
kira::sound::static_sound::PlaybackState::Playing => {
PlaybackState::Playing { position }
}
kira::sound::static_sound::PlaybackState::Paused => PlaybackState::Paused { position },
kira::sound::static_sound::PlaybackState::Stopped => PlaybackState::Stopped,
kira::sound::static_sound::PlaybackState::Pausing => {
PlaybackState::Pausing { position }
}
kira::sound::static_sound::PlaybackState::Stopping => {
PlaybackState::Stopping { position }
}
}
}
}

/// Extension trait to add new audio channels to the application
pub trait AudioApp {
/// Add a new audio channel to the application
Expand Down
29 changes: 6 additions & 23 deletions src/audio_output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::audio::{
AudioCommand, AudioCommandResult, InstanceHandle, PlayAudioSettings, PlaybackState,
};
use crate::audio::{AudioCommand, AudioCommandResult, InstanceHandle, PlayAudioSettings};
use bevy::prelude::*;
use std::any::TypeId;

Expand All @@ -24,9 +22,9 @@ pub struct AudioOutput {
channels: HashMap<TypeId, ChannelState>,
}

struct InstanceState {
kira: StaticSoundHandle,
handle: InstanceHandle,
pub(crate) struct InstanceState {
pub(crate) kira: StaticSoundHandle,
pub(crate) handle: InstanceHandle,
}

impl FromWorld for AudioOutput {
Expand Down Expand Up @@ -301,26 +299,11 @@ pub(crate) fn update_instance_states<T: Resource>(
mut channel: ResMut<AudioChannel<T>>,
) {
if let Some(instances) = audio_output.instances.get(&TypeId::of::<T>()) {
channel.states.clear();
for instance_state in instances.iter() {
let position = instance_state.kira.position();
let playback_status = match instance_state.kira.state() {
kira::sound::static_sound::PlaybackState::Playing => {
PlaybackState::Playing { position }
}
kira::sound::static_sound::PlaybackState::Paused => {
PlaybackState::Paused { position }
}
kira::sound::static_sound::PlaybackState::Stopped => PlaybackState::Stopped,
kira::sound::static_sound::PlaybackState::Pausing => {
PlaybackState::Pausing { position }
}
kira::sound::static_sound::PlaybackState::Stopping => {
PlaybackState::Stopping { position }
}
};
channel
.states
.insert(instance_state.handle.clone(), playback_status);
.insert(instance_state.handle.clone(), instance_state.into());
}
}
}

0 comments on commit 63d4f82

Please sign in to comment.