]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/Sound.py
reSWIGged
[wxWidgets.git] / wxPython / demo / Sound.py
1
2 import wx
3
4 from Main import opj
5
6 #----------------------------------------------------------------------
7
8 class TestPanel(wx.Panel):
9 def __init__(self, parent, log):
10 wx.Panel.__init__(self, parent, -1)
11 self.log = log
12
13 b = wx.Button(self, -1, "Play Sound 1 (sync)", (25, 25))
14 self.Bind(wx.EVT_BUTTON, self.OnButton1, b)
15
16 b = wx.Button(self, -1, "Play Sound 2 (async)", (25, 65))
17 self.Bind(wx.EVT_BUTTON, self.OnButton2, b)
18
19 b = wx.Button(self, -1, "Select .WAV file", (25, 105))
20 self.Bind(wx.EVT_BUTTON, self.OnSelectSound, b)
21
22
23 def OnButton1(self, evt):
24 try:
25 self.sound = wx.Sound(opj('data/anykey.wav'))
26 self.log.write("before Play...\n")
27 self.sound.Play(wx.SOUND_SYNC)
28 self.log.write("...after Play\n")
29 except NotImplementedError, v:
30 wx.MessageBox(str(v), "Exception Message")
31
32
33 def OnButton2(self, evt):
34 try:
35 if True:
36 self.sound = wx.Sound(opj('data/plan.wav'))
37 else:
38 # sounds can also be loaded from a buffer object
39 data = open(opj('data/plan.wav'), 'rb').read()
40 self.sound = wx.SoundFromData(data)
41
42 self.log.write("before Play...\n")
43 self.sound.Play(wx.SOUND_ASYNC)
44 wx.YieldIfNeeded()
45 self.log.write("...after Play\n")
46 except NotImplementedError, v:
47 wx.MessageBox(str(v), "Exception Message")
48
49
50 def OnSelectSound(self, evt):
51 dlg = wx.FileDialog(wx.GetTopLevelParent(self),
52 "Choose a sound file",
53 wildcard="WAV files (*.wav)|*.wav",
54 style=wx.OPEN)
55 if dlg.ShowModal() == wx.ID_OK:
56 try:
57 #self.sound = wx.Sound(dlg.GetPath())
58 #self.sound.Play()
59 wx.Sound.PlaySound(dlg.GetPath())
60 except NotImplementedError, v:
61 wx.MessageBox(str(v), "Exception Message")
62 dlg.Destroy()
63
64
65 #----------------------------------------------------------------------
66
67 def runTest(frame, nb, log):
68 win = TestPanel(nb, log)
69 return win
70
71 #----------------------------------------------------------------------
72
73
74 overview = """<html><body>
75 <h2>Sound</h2>
76 This class represents a short wave file, in Windows WAV format, that can
77 be stored in memory and played. Currently this class is implemented on Windows
78 and GTK (Linux) only.
79 <p>
80 This demo offers two examples, both driven by buttons, but obviously the event
81 that drives the playing of the sound can come from anywhere.
82
83 </body></html>
84 """
85
86
87 if __name__ == '__main__':
88 import sys,os
89 import run
90 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])