Monday, 17 May 2021

Audio position does not change in kivy

I am creating a music player in kivy using kivy.core.audio.Sounloader and I have come across a problem. When the user hits the start button I want them to be able to slide the slider and change the position of the song, however, the function just doesn't work. It also does not raise any errors, it just doesn't work. I also tried getting the position of the song with music_obj.get_pos() but it always returned 0. I did a couple of searches and others seem to have this problem too. How can I solve this? My code:

from kivy.app import App
from kivy.core.audio import SoundLoader
from kivy.uix.widget import Widget
from kivy.lang import Builder


Builder.load_string('''
<MyLayout>:
    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height

        Label:
            id: song_title
            text: "Song title!"
            text_size: self.size
            font_size: 32
            valign: "middle"
            halign: "center"

        Slider:
            id: slider
            min: 0
            max: 1
            step: 1
            value: 0
            on_value: root.change_pos(self.value)

        Button:
            text: "Animate!"
            font_size: 32
            on_release: root.start_song()''')


class MyLayout(Widget):
    music_file = "pitbull-feat-kesha-pitbull-feat-keshatimber.mp3"
    music_obj = None

    def start_song(self):
        self.music_obj = SoundLoader.load(self.music_file)
        if self.music_obj:
            print(self.music_obj.source)
            print(self.music_obj.length)
            self.ids.song_title.text = self.music_obj.source
            self.ids.slider.max = self.music_obj.length
            self.music_obj.play()

    def change_pos(self, value):
        if self.music_obj is not None:
            self.music_obj.seek(value)


class Awesome(App):
    def build(self):
        self.title = "Hello!"
        return MyLayout()


if __name__ == "__main__":
    Awesome().run()


from Audio position does not change in kivy

No comments:

Post a Comment