Jerry's method is probably the best way to do it. In your sample code, you were adding the file every time the music is changed, which is inefficient. So if you changed the music 5 times, you would have created 6 audio objects, which would waste your system resources.
Also, the error your were receiving was probably due to the fact that you were not declaring the
music variable as
global. In Python, if you assign a value to a global variable inside a function, you must declare that variable as
global. Here is sample code:
Code:
def ChangeToFirst():
global music
music.rewind()
music = viz.add('song1.mp3')
music.play()
def ChangeToSecond():
global music
music.rewind()
music = viz.add('song2.mp3')
music.play()