Vizard 7 » Tutorials & Examples » Multimedia » Multimedia (Audio & Video)
7.6

Tutorial: Multimedia (Audio & Video)

This tutorial will demonstrate how to add audio and video to a scene. First, we will see how to add sound to the world.  Then, we'll add sound localized to an object in the world. Finally, we will apply a video as a texture to a model.

Note: this content is discussed in more detail in the reference section.

Setting up a media world

If you have trouble getting the code in this tutorial to work, you can find the complete example script multimedia.py in the \tutorials\multimedia directory.

 

First create an empty script and add the following initial code to it:

import viz
import vizact

viz.setMultiSample(4)
viz.fov(60)
viz.go()

viz.clearcolor(viz.SKYBLUE)

Add and play an audio file

To add an audio file to your world, use the following line:

mySound = viz.addAudio( 'bach_air.mid' )

Here we've added the audio file 'bach_air.mid'. In general, Vizard will support any audio format that your computer supports. Once you've added a sound file, you can play it, pause it, or stop it.  Next let's add the ability to play the file when we hit the 'p' key and stop it when we hit the 's' key.

vizact.onkeydown( 'p', mySound.play )
vizact.onkeydown( 's', mySound.stop )

Now run the script and try those keypresses. You can also start a sound file from any point or at any speed. Close the world and add a couple more lines to your script to make the sound file play in a loop at three times its normal speed.

mySound.loop( viz.ON )
mySound.rate(3)

Now run your script again and see how it sounds once you hit the 'p' key.

Add localized sound

You can also add localized sound to objects in your world such that the sound appears to come from that object.  In the next part of the script we'll add a quacking duck to the world. In the following code,  the first two lines add the duck and set its orientation.  The two lines after that create and then apply an action to the duck that moves it back and forth.

#add localized sound
duck = viz.addAvatar( 'duck.cfg' )
duck.setEuler([180,0,0])
duckMove = vizact.sequence( [vizact.moveTo([0,1,22],speed=2), vizact.moveTo([0,1,0],speed=2)], viz.FOREVER)
duck.addAction(duckMove)

In the next line we'll add a quacking sound to the duck. By including the viz.STOP flag, we'll keep the duck from quacking until we're ready.

quack = duck.playsound( 'quack.wav', viz.STOP )

We'll use the 'q' key to start the duck's quacking. The play command will also loop the quacking sound.

vizact.onkeydown( 'q', quack.play, viz.LOOP )

Now run the script and hit 'q'. You should be able to hear the quack get louder and softer as he gets closer and further away.  

Add and play a video file

Next, use the following line to add a video file to your world.

myVideo = viz.addVideo( 'mona.mpg' )

Then we'll need something to play the video on, like a screen, within our world. So, let's add a ball and then texture the ball with the video. We'll also add a couple lines so that the ball is in a position and orientation where we can see the video.

#use ball as screen and texture with video
myScreen = viz.addChild('white_ball.wrl')
myScreen.texture( myVideo )
myScreen.setPosition([0.5,2,2])
myScreen.setEuler([-90,90,0])

Finally, let's play the video on loop.

myVideo.play()
myVideo.loop()

Now run your script again to see the video in action.