Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use iced::{
- advanced::Application,
- executor,
- program::{Appearance, DefaultStyle},
- widget::{button, container, Column, Row},
- Command, Renderer, Settings, Theme,
- };
- use kira::{
- manager::{backend::DefaultBackend, AudioManager, AudioManagerSettings},
- sound::static_sound::{StaticSoundData, StaticSoundSettings},
- };
- use std::error::Error;
- pub fn main() -> iced::Result {
- Looper::run(Settings::default())
- }
- struct Looper {
- sound: Option<StaticSoundData>,
- }
- #[derive(Debug, Clone)]
- enum Message {
- File,
- Play(Option<StaticSoundData>),
- }
- impl Application for Looper {
- type Executor = executor::Default;
- type Message = Message;
- type Theme = Theme;
- type Flags = ();
- type Renderer = Renderer;
- fn new(_flags: Self::Flags) -> (Self, iced::Command<Self::Message>) {
- (Self { sound: None }, Command::none())
- }
- fn title(&self) -> String {
- String::from("Loop")
- }
- fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
- match message {
- Message::File => Command::perform(get_file(), Message::Play),
- Message::Play(s) => {
- self.sound = s.clone();
- play_sound(self.sound.clone().unwrap().clone()).ok();
- Command::none()
- }
- Message::Play(None) => Command::none(),
- }
- }
- fn view(&self) -> iced::Element<'_, Self::Message, Self::Theme, Self::Renderer> {
- let layout = Row::new();
- let con = Column::new().push(button("Fetch").on_press(Message::File));
- container(layout.push(con)).into()
- }
- fn theme(&self) -> Self::Theme {
- Self::Theme::default()
- }
- fn style(&self, theme: &Self::Theme) -> Appearance {
- theme.default_style()
- }
- }
- async fn get_file() -> Option<StaticSoundData> {
- println!("getting the file..");
- let sound_data = StaticSoundData::from_file("kr.mp3", StaticSoundSettings::default());
- match sound_data {
- Ok(s) => Some(s),
- Err(_) => None,
- }
- }
- fn play_sound(sd: StaticSoundData) -> Result<(), Box<dyn Error>> {
- println!("i should play the file");
- let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
- manager.play(sd.clone())?;
- Ok(())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement