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