omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Play MP3 from UI doesn't work, sound module

    Pythonista
    3
    3
    2820
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cook
      cook last edited by

      I have a UI with a buttonitem like this:

      play_b = ui.ButtonItem(title='play',action=playmp3,enabled=True)
      

      And then it calls to this function:

      def playmp3(sender):
          player = sound.Player('filename')
          player.play()
      

      Anyway- it won't play.
      If I write the same code without a function it does play. But inside a function (UI or no UI) it won't play.

      What can I do differently to get this working?

      1 Reply Last reply Reply Quote 0
      • Webmaster4o
        Webmaster4o last edited by

        Are there errors? Also, this might require the @ui.in_background decorator.

        1 Reply Last reply Reply Quote 0
        • omz
          omz last edited by

          Try declaring the player globally.

          player = sound.Player('filename')
          
          def playmp3(sender):    
              player.play()
          

          In your code, player is a local variable in the playmp3 function that gets garbage-collected as soon as the function returns. When the player's memory is deallocated, it also stops playing of course.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB Forums | Contributors