PDA

View Full Version : Sound doesn't play from director functions


roman_suvorov
07-26-2008, 02:36 PM
Hello, when running a sample script like this one:


import viz
viz.go()

def playSound():
sound = viz.addAudio("quack.wav")
sound.play()
return

viz.director(playSound)


.. no sound is played and the following error message is displayed:


** ERROR: Failed to create graph builder (CoInitialize has not been called. )
** ERROR: Failed to load audio 'quack.wav'


However, this works fine:

import viz
viz.go()
sound = viz.addAudio("quack.wav")
sound.play()


.. so I figured this must have something to do with Vizard's handling of director functions. Any ideas?

farshizzo
07-29-2008, 09:59 AM
You need to add the sound object in the main thread. Once it is added you should be able to play it in other director threads.

If it is necessary for you to add the sound object in the director thread, then you can install the pywin32 library for python and use the following code:import viz
import pythoncom
viz.go()

def playSound():

pythoncom.CoInitialize() #Initialize COM

sound = viz.addAudio("quack.wav")
sound.play()

pythoncom.CoUninitialize() #Uninitialize COM

return

viz.director(playSound)

Gladsomebeast
07-29-2008, 12:25 PM
If it takes a while to add the sound, would there be a frame rate blip on the add?

What if you added a 3D model with this method?

farshizzo
07-29-2008, 02:28 PM
If the audio takes a while to load, then adding the file during runtime will cause a stall in the framerate. In this case, you would use the modified version of the director function I posted above. This is not the case for 3D models though, they can be added as usual in director functions. The call to pythoncom.CoInitialize() is only necessary for COM/DirectX related objects.

roman_suvorov
07-31-2008, 08:31 AM
Thanks farshizzo, very helpful as usual. Works flawlessly now. :)