Is there a way to make sound play through different screens in kivy?

Viewed 29

Is there a way to make sound play through different screens? Right now, the sound stops playing for me once you go to a different screen. I want the sound to continue playing, even when screens are changing and it goes to a different screen.Here's the code I used for this: .py code:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.core.audio import SoundLoader
from kivy.lang import Builder

Builder.load_file("test1.kv")

class a62(Screen):
    def on_enter(self, *args):
        Clock.schedule_once(self.play_sound, 1)
        Clock.schedule_once(self.next_screen4, 1.5) #music.mp3 needs to continue playing even when it reaches a17 and 18
        Clock.schedule_once(self.next_screen5, 2.5)

    def play_sound(self, dt):
        sound = SoundLoader.load('sound/music.mp3')  
        if sound:
            print("playing sound123")
            sound.volume = 1
            sound.play()

    def next_screen4(self, dt):
        self.manager.current = "a17"

    def next_screen5(self, dt):
        self.manager.current = "a18"


class a17(Screen):
    pass


class a18(Screen):
    pass


class CandDApp(App):

    def build(self):
        sm = ScreenManager()
        sm.add_widget(a62(name="a62"))
        sm.add_widget(a17(name="a17"))
        sm.add_widget(a18(name="a18"))
        return sm


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

.kv code:

<a62>:
    Button:
        text: "a62"
<a17>:
    Button:
        text: "a17"
<a18>:
    Button:
        text: "a18"
0 Answers
Related