Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Playing music in background



  • Hello.
    I want to play music in list in background and have a control on stop theme. when i play the music my Qt form doesn't response to other function. Should i implement QThread How ?

            for i in listChecked:
                self.playsoundQu(i)
    

    and the function :

        def playsoundQu(self,filename):
            filename = "Downloads/"+filename
            vlc_instance = vlc.Instance()
            media = vlc_instance.media_new(filename)
            player = vlc_instance.media_player_new()
            player.set_media(media)
            player.play()
            time.sleep(1.5)
            duration = player.get_length() / 1000
            time.sleep(duration)
    

    It works but form doen't response to other action and is somehow locked.


  • Moderators

    @behruz-montazeri
    I don't know much about python, but my guess is, that "time.sleep" is causing the "freeze" of your Gui, as it will do in pure c++


  • Banned

    Actually the whole thing is going to freeze up your Gui because Python is linear and even if you were to create another Thread it would still process things linearly due to the GIL issue. As such anytime you are going to create an endlessly running loop you need to either shoot it off into its own multi-process (which will then have the issue if it does not self-end) or implement some means to pass back event handling to the main event thread from time to time -- the actual solution however greatly depends upon the application's purpose and use


  • Lifetime Qt Champion

    @behruz-montazeri Make player a member variable, so it does not disappear when playsoundQu finishes and then remove all the sleep() calls from playsoundQu...


Log in to reply