Forum Archive

Play MP3 from UI doesn't work, sound module

cook

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?

Webmaster4o

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

omz

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.