Open topic with navigation
Adding sounds
\examples\multimedia\sounds.py
This script demonstrates how to play regular sound files.
"""
This script demonstrates how to play regular sound files.
Use the buttons on screen to play, pause, and stop the sound.
You can also toggle between looping the sound.
The slider displays the current position of the sound.
"""
import viz
import vizact
import vizdlg
import vizinfo
viz.go()
vizinfo.InfoPanel(align=viz.ALIGN_RIGHT_TOP)
#Add the sound
sound = viz.addAudio('sounds/platform_start.wav')
#Add control panel
controlPanel = vizinfo.InfoPanel(text=None, title='Sound', icon=False, align=viz.ALIGN_CENTER)
time_progress = controlPanel.addLabelItem('Time', viz.addProgressBar(''))
loop_checkbox = controlPanel.addLabelItem('Loop', viz.addCheckbox())
play_button = viz.addButtonLabel('Play')
pause_button = viz.addButtonLabel('Pause')
stop_button = viz.addButtonLabel('Stop')
controlPanel.addLabelItem('', vizinfo.RowGroup([play_button, pause_button, stop_button]))
#Update the progress based on sound time
duration = sound.getDuration()
def update_progress():
time_progress.message('{:.1f} / {:.1f}'.format(sound.getTime(), duration))
time_progress.set(sound.getTime() / duration)
vizact.ontimer(0, update_progress)
#Setup GUI events
vizact.onbuttondown(loop_checkbox, sound.loop, True)
vizact.onbuttonup(loop_checkbox, sound.loop, False)
vizact.onbuttondown(play_button, sound.play) #Play the sound (from current position)
vizact.onbuttondown(pause_button, sound.pause) #Pause the sound (saves current position)
vizact.onbuttondown(stop_button, sound.stop) #Stop the sound (resets position back to beginning)