Advertisement
jevixlugya

Untitled

Feb 12th, 2023
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. from kivy.core.audio import SoundLoader
  4. from kivy.uix.boxlayout import BoxLayout
  5. from kivy.properties import ObjectProperty, StringProperty
  6. from kivy.clock import Clock
  7. from pathlib import Path
  8.  
  9. kv = """
  10. RootBoxLayout:
  11.    orientation: 'vertical'
  12.    AnchorLayout:
  13.        ToggleButton:
  14.            size_hint: None, None
  15.            size: 200, 48
  16.            text: {'normal': 'Play', 'down': 'Stop'}[self.state]
  17.            on_state: root.control(self.state)
  18.    BoxLayout:
  19.        size_hint_y: None
  20.        height: 48
  21.        padding:25
  22.        ProgressBar:
  23.            id:progress
  24.            max:100
  25.  
  26.        Label:
  27.            id: title
  28.        Label:
  29.            id: length
  30.        Label:
  31.            text: root.current
  32. """
  33.  
  34. class RootBoxLayout(BoxLayout):
  35.     song = ObjectProperty()
  36.     current = StringProperty()
  37.  
  38.     def __init__(self, **kwargs):
  39.         super().__init__(**kwargs)
  40.         self.schedule = None
  41.         self.song = SoundLoader.load('piano/4.mp3')
  42.  
  43.     def control(self, state):
  44.         if state == 'down':
  45.             self.song.play()
  46.             self.ids.title.text = Path(self.song.source).name
  47.             self.ids.length.text = f'length: {self.song.length:0.1f} seconds'
  48.             self.schedule = Clock.schedule_interval(self._update_pos, .1)
  49.             self.ids.progress.value+=10
  50.         else:
  51.             self.song.stop()
  52.             self.schedule.cancel()
  53.  
  54.     def _update_pos(self, _):
  55.         self.current = f'{self.song.get_pos():0.1f} seconds'
  56.  
  57.  
  58. class SoundTestApp(App):
  59.     def build(self):
  60.         return Builder.load_string(kv)
  61.  
  62.  
  63. SoundTestApp().run()  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement