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
-
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?
-
Are there errors? Also, this might require the
@ui.in_background
decorator. -
Try declaring the
player
globally.player = sound.Player('filename') def playmp3(sender): player.play()
In your code,
player
is a local variable in theplaymp3
function that gets garbage-collected as soon as the function returns. When the player's memory is deallocated, it also stops playing of course.