use std::path::PathBuf;
use sdl2::mixer::{self, Music};
pub struct CoreAudio { music: Option<Music<'static>>, sounds: Vec<mixer::Chunk> }
impl CoreAudio {
pub(crate) fn new(sound_count: u16) -> CoreAudio {
let sounds: Vec<_> = (0..sound_count)
.map(|id| PathBuf::from(format!("assets/sound{}.ogg", id)))
.map(|p| mixer::Chunk::from_file(p).unwrap())
.collect();
CoreAudio { sounds, music: None }
}
pub fn play_sound(&mut self, sound: u16) {
mixer::Channel::all().play(&self.sounds[sound as usize], 0).unwrap();
}
pub fn play_music(&mut self, music: u16, loops: bool) {
let music = &format!("assets/music{}.ogg", music);
self.stop_music();
let music = mixer::Music::from_file(music).unwrap();
music.play(if loops { 1_000_000 } else { 1 }).unwrap();
self.music = Some(music);
}
pub fn stop_music(&mut self) {
if let Some(_) = self.music.take() {
Music::halt();
}
}
}