#1
|
|||
|
|||
Basic soundtrack
I'm trying to learn the basics of scripting in Vizard/Python and I'm having some confusion. The code below is intended to just start, stop, or change the background music, but it's getting errors that I suppose must stem from me not understanding variable declarations. If someone could give me a push in the right direction I'd really appreciate it.
music = viz.add('song1.mp3') music.loop(viz.ON) def ChangeToFirst(): music.rewind() music = viz.add('song1.mp3') music.play() def ChangeToSecond(): music.rewind() music = viz.add('song2.mp3') music.play() def StopTheMusic(): music.rewind() def mykey(key): if key == '1': ChangeToFirst() elif key == '2': ChangeToSecond() elif key == ' ': StopTheMusic() |
#2
|
|||
|
|||
Here's how I would do that.
m1 = viz.add('c:/winnt/media/canyon.mid') m1.loop() m2 = viz.add('c:/winnt/media/flourish.mid') m2.loop() def onkeydown(key): if key == '1': m2.stop() m1.play() if key == '2': m1.stop() m2.play() |
#3
|
|||
|
|||
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() |
#4
|
|||
|
|||
Ah, the "global" varable problem. The first sound file you create and assign to the "music" varable is a global varable.
When you use the equals sign in a fuction, it creates a *new and local* varable which only lives within the function. So your "Change" fuctions are trying to create new sound objects and playing them without reasigning the global music function. You are probably getting the "accessing varable before instatiated" error here because you are trying to rewind() the music varable, then assigning to it. The following code avoids this by added a 'currentMusic' parameter to the fuctions which takes as an argument the global music varable: Code:
music = viz.add('song.mp3') music.play() def stopOldAndPlayNewSong(currentMusic, newSongFile): currentMusic.stop() currentMusic = viz.add(newSongFile) currentMusic.play() def onkey(key): key == '1': stopOldAndPlayNewSong(music, 'song1.mp3') Rather than diving right into Vizard programming, I strongly suggest investing in your python skills. Here is an exclent tutorial: http://docs.python.org/tut/
__________________
Paul Elliott WorldViz LLC |
#5
|
|||
|
|||
Triple jinks!!!
__________________
Paul Elliott WorldViz LLC |
#6
|
|||
|
|||
It's spelled jinx. I guess we get off on a technicality. Your powers, they do nothing!!!
|
#7
|
|||
|
|||
Thanks so much. I'm reading Dive Into Python, but it's coming to me at a frustratingly slow pace. Why can't everything be programmed in C++? Sad.
Thanks for explaining about the global variables, that will come in handy in all sorts of things. |
Thread Tools | |
Display Modes | Rate This Thread |
|
|