Advertisement
Guest User

help

a guest
Apr 25th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. use iced::{
  2. advanced::Application,
  3. executor,
  4. program::{Appearance, DefaultStyle},
  5. widget::{button, container, Column, Row},
  6. Command, Renderer, Settings, Theme,
  7. };
  8. use kira::{
  9. manager::{backend::DefaultBackend, AudioManager, AudioManagerSettings},
  10. sound::static_sound::{StaticSoundData, StaticSoundSettings},
  11. };
  12. use std::error::Error;
  13.  
  14. pub fn main() -> iced::Result {
  15. Looper::run(Settings::default())
  16. }
  17.  
  18. struct Looper {
  19. sound: Option<StaticSoundData>,
  20. }
  21.  
  22. #[derive(Debug, Clone)]
  23. enum Message {
  24. File,
  25. Play(Option<StaticSoundData>),
  26. }
  27.  
  28. impl Application for Looper {
  29. type Executor = executor::Default;
  30. type Message = Message;
  31. type Theme = Theme;
  32. type Flags = ();
  33. type Renderer = Renderer;
  34.  
  35. fn new(_flags: Self::Flags) -> (Self, iced::Command<Self::Message>) {
  36. (Self { sound: None }, Command::none())
  37. }
  38.  
  39. fn title(&self) -> String {
  40. String::from("Loop")
  41. }
  42.  
  43. fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
  44. match message {
  45. Message::File => Command::perform(get_file(), Message::Play),
  46. Message::Play(s) => {
  47. self.sound = s.clone();
  48. play_sound(self.sound.clone().unwrap().clone()).ok();
  49. Command::none()
  50. }
  51. Message::Play(None) => Command::none(),
  52. }
  53. }
  54.  
  55. fn view(&self) -> iced::Element<'_, Self::Message, Self::Theme, Self::Renderer> {
  56. let layout = Row::new();
  57. let con = Column::new().push(button("Fetch").on_press(Message::File));
  58.  
  59. container(layout.push(con)).into()
  60. }
  61.  
  62. fn theme(&self) -> Self::Theme {
  63. Self::Theme::default()
  64. }
  65.  
  66. fn style(&self, theme: &Self::Theme) -> Appearance {
  67. theme.default_style()
  68. }
  69. }
  70.  
  71. async fn get_file() -> Option<StaticSoundData> {
  72. println!("getting the file..");
  73. let sound_data = StaticSoundData::from_file("kr.mp3", StaticSoundSettings::default());
  74. match sound_data {
  75. Ok(s) => Some(s),
  76. Err(_) => None,
  77. }
  78. }
  79.  
  80. fn play_sound(sd: StaticSoundData) -> Result<(), Box<dyn Error>> {
  81. println!("i should play the file");
  82. let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
  83. manager.play(sd.clone())?;
  84. Ok(())
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement