]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/Sound.py
   6 #---------------------------------------------------------------------- 
   8 class TestPanel(wx
.Panel
): 
   9     def __init__(self
, parent
, log
): 
  10         wx
.Panel
.__init
__(self
, parent
, -1) 
  13         b 
= wx
.Button(self
, -1, "Play Sound 1 (sync)", (25, 25)) 
  14         self
.Bind(wx
.EVT_BUTTON
, self
.OnButton1
, b
) 
  16         b 
= wx
.Button(self
, -1, "Play Sound 2 (async)", (25, 65)) 
  17         self
.Bind(wx
.EVT_BUTTON
, self
.OnButton2
, b
) 
  19         b 
= wx
.Button(self
, -1, "Select .WAV file", (25, 105)) 
  20         self
.Bind(wx
.EVT_BUTTON
, self
.OnSelectSound
, b
) 
  23     def OnButton1(self
, evt
): 
  25             sound 
= wx
.Sound(opj('data/anykey.wav')) 
  26             self
.log
.write("before Play...\n") 
  27             sound
.Play(wx
.SOUND_SYNC
) 
  28             self
.log
.write("...after Play\n") 
  29         except NotImplementedError, v
: 
  30             wx
.MessageBox(str(v
), "Exception Message") 
  33     def OnButton2(self
, evt
): 
  36                 sound 
= wx
.Sound(opj('data/plan.wav')) 
  38                 # sounds can also be loaded from a buffer object 
  39                 data 
= open(opj('data/plan.wav'), 'rb').read() 
  40                 sound 
= wx
.SoundFromData(data
) 
  42             self
.log
.write("before Play...\n") 
  43             sound
.Play(wx
.SOUND_ASYNC
) 
  44             self
.sound 
= sound  
# save a reference (This shoudln't be needed, but there seems to be a bug...) 
  46             self
.log
.write("...after Play\n") 
  47         except NotImplementedError, v
: 
  48             wx
.MessageBox(str(v
), "Exception Message") 
  51     def OnSelectSound(self
, evt
): 
  52         dlg 
= wx
.FileDialog(wx
.GetTopLevelParent(self
), 
  53                             "Choose a sound file", 
  54                             wildcard
="WAV files (*.wav)|*.wav", 
  56         if dlg
.ShowModal() == wx
.ID_OK
: 
  58                 #sound = wx.Sound(dlg.GetPath(), wx.SOUND_SYNC) 
  61                 # another way to do it. 
  62                 wx
.Sound
.PlaySound(dlg
.GetPath(), wx
.SOUND_SYNC
) 
  64             except NotImplementedError, v
: 
  65                 wx
.MessageBox(str(v
), "Exception Message") 
  69 #---------------------------------------------------------------------- 
  71 def runTest(frame
, nb
, log
): 
  72     win 
= TestPanel(nb
, log
) 
  75 #---------------------------------------------------------------------- 
  78 overview 
= """<html><body> 
  80 This class represents a short wave file, in Windows WAV format, that can 
  81 be stored in memory and played. Currently this class is implemented on Windows 
  84 This demo offers two examples, both driven by buttons, but obviously the event 
  85 that drives the playing of the sound can come from anywhere. 
  91 if __name__ 
== '__main__': 
  94     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])