自己寫的鬧鐘, 只可以播放wav格式的音頻。
- import time
- import sys
-
- soundFile = 'sound.wav'
- not_executed = 1
-
- def soundStart():
- if sys.platform[:5] == 'linux':
- import os
- os.popen2('aplay -q' + soundFile)
- else:
- import winsound
- winsound.PlaySound(soundFile, winsound.SND_FILENAME)
-
- while(not_executed):
- dt = list(time.localtime())
- hour = dt[3]
- minute = dt[4]
- if hour == 17 and minute == 38:
- soundStart()
- not_executed = 0
winsound 模塊提供訪問由 Windows 平臺提供的基本的聲音播放設(shè)備。它包含函數(shù)和數(shù)個常量。
Beep(frequency, duration)
蜂鳴PC的喇叭。 frequency 參數(shù)指定聲音的頻率,以赫茲,并且必須是在 37 到 32,767
的范圍之中。duration 參數(shù)指定聲音應(yīng)該持續(xù)的毫秒數(shù)。如果系統(tǒng)不能蜂鳴喇叭,掛起 RuntimeError。注意:Windows 95 和 98下,Windows Beep() 函數(shù)存在但是無效的(它忽略它的參數(shù))。這種情況下Python通過直接的端口操作模擬它(2.1版本中增加的)。不知道是否在所有的系統(tǒng)上都工作。1.6版本中的新特性。
PlaySound(sound, flags)
從平臺 API 中調(diào)用 PlaySound() 函數(shù)。sound 參數(shù)必須是一個文件名,音頻數(shù)據(jù)作為字符串,或為 None。它的解釋依賴于 flags 的值,該值可以是一個位方式或下面描述的變量的組合。如果系統(tǒng)顯示一個錯誤,掛起 RuntimeError 。
MessageBeep([type=MB_OK])
從平臺 API 中調(diào)用 MessageBeep() 函數(shù)。播放一個在注冊表中指定的聲音。type 參數(shù)指定播放哪一個聲音;可能的值是 -1,MB_ICONASTERISK,MB_ICONEXCLAMATION,MB_ICONHAND,MB_ICONQUESTION,和 MB_OK,所有的描述如下。值 -1 產(chǎn)生一個``簡單的蜂鳴'';換句話說這是如果聲音不能被播放的后備計劃。2.3版本中的新特性。
SND_FILENAME
sound 參數(shù)是一個 WAV 文件的名稱。不使用 SND_ALIAS。
SND_ALIAS
sound 參數(shù)是注冊表中一個聲音組合的名稱。如果注冊表沒有包含這樣的名稱,播放系統(tǒng)缺省的聲音除非 SND_NODEFAULT 也被指定。如果沒有缺省的聲音被注冊,掛起 RuntimeError。不使用 SND_FILENAME。
所有的 Win32 系統(tǒng)至少支持下列,大多數(shù)系統(tǒng)支持的更多:
PlaySound() 名稱 對應(yīng)的控制面板聲音名稱
'SystemAsterisk' Asterisk
'SystemExclamation' Exclamation
'SystemExit' Exit Windows
'SystemHand' Critical Stop
'SystemQuestion' Question
例子:
- import winsound
-
-
- winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
-
-
-
- winsound.PlaySound("*", winsound.SND_ALIAS)
SND_LOOP
重復(fù)地播放聲音。SND_ASYNC標(biāo)識也必須被用來避免堵塞。不能用 SND_MEMORY。
SND_MEMORY
提供給PlaySound()的 sound 參數(shù)是一個 WAV 文件的內(nèi)存映像(memory image),作為一個字符串。
注意:這個模塊不支持從內(nèi)存映像中異步播放,因此這個標(biāo)識和 SND_ASYNC 的組合將掛起 RuntimeError。
SND_PURGE
停止播放所有指定聲音的實例。
SND_ASYNC
立即返回,允許聲音異步播放。
SND_NODEFAULT
不過指定的聲音沒有找到,不播放系統(tǒng)缺省的聲音。
SND_NOSTOP
不中斷當(dāng)前播放的聲音。
SND_NOWAIT
如果聲音驅(qū)動忙立即返回。
MB_ICONASTERISK
播放 SystemDefault 聲音。
MB_ICONEXCLAMATION
播放 SystemExclamation 聲音。
MB_ICONHAND
播放 SystemHand 聲音。
MB_ICONQUESTION
播放 SystemQuestion 聲音。
MB_OK
播放 SystemDefault 聲音。
實例一
- import wx
- from wx.lib.filebrowsebutton import FileBrowseButton
-
- class MyFrame(wx.Frame):
- def __init__(self):
- wx.Frame.__init__(self, None, title="wx.Sound",size=(500,100))
- p = wx.Panel(self)
-
- self.fbb = FileBrowseButton(p,labelText="Select WAV file:",fileMask="*.wav")
- btn = wx.Button(p, -1, "Play")
- self.Bind(wx.EVT_BUTTON, self.OnPlaySound, btn)
-
- sizer = wx.BoxSizer(wx.HORIZONTAL)
- sizer.Add(self.fbb, 1, wx.ALIGN_CENTER_VERTICAL)
- sizer.Add(btn, 0, wx.ALIGN_CENTER_VERTICAL)
- border = wx.BoxSizer(wx.VERTICAL)
- border.Add(sizer, 0, wx.EXPAND|wx.ALL, 15)
- p.SetSizer(border)
-
-
- def OnPlaySound(self, evt):
- filename = self.fbb.GetValue()
- self.sound = wx.Sound(filename)
- if self.sound.IsOk():
- self.sound.Play(wx.SOUND_ASYNC)
- else:
- wx.MessageBox("Invalid sound file", "Error")
-
-
- app = wx.PySimpleApp()
- frm = MyFrame()
- frm.Show()
- app.MainLoop()
實例二
- import wx
- import wx.media
- import os
-
- class Panel1(wx.Panel):
- def __init__(self, parent, id):
-
- wx.Panel.__init__(self, parent, -1, style=wx.TAB_TRAVERSAL|wx.CLIP_CHILDREN)
-
-
- try:
- self.mc = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER)
- except NotImplementedError:
- self.Destroy()
- raise
-
- loadButton = wx.Button(self, -1, "Load File")
- self.Bind(wx.EVT_BUTTON, self.onLoadFile, loadButton)
-
- playButton = wx.Button(self, -1, "Play")
- self.Bind(wx.EVT_BUTTON, self.onPlay, playButton)
-
- pauseButton = wx.Button(self, -1, "Pause")
- self.Bind(wx.EVT_BUTTON, self.onPause, pauseButton)
-
- stopButton = wx.Button(self, -1, "Stop")
- self.Bind(wx.EVT_BUTTON, self.onStop, stopButton)
-
- slider = wx.Slider(self, -1, 0, 0, 0, size=wx.Size(300, -1))
- self.slider = slider
- self.Bind(wx.EVT_SLIDER, self.onSeek, slider)
-
- self.st_file = wx.StaticText(self, -1, ".mid .mp3 .wav .au .avi .mpg", size=(200,-1))
- self.st_size = wx.StaticText(self, -1, size=(100,-1))
- self.st_len = wx.StaticText(self, -1, size=(100,-1))
- self.st_pos = wx.StaticText(self, -1, size=(100,-1))
-
-
- sizer = wx.GridBagSizer(5,5)
- sizer.Add(loadButton, (1,1))
- sizer.Add(playButton, (2,1))
- sizer.Add(pauseButton, (3,1))
- sizer.Add(stopButton, (4,1))
- sizer.Add(self.st_file, (1, 2))
- sizer.Add(self.st_size, (2, 2))
- sizer.Add(self.st_len, (3, 2))
- sizer.Add(self.st_pos, (4, 2))
- sizer.Add(self.mc, (5,1), span=(5,1))
- self.SetSizer(sizer)
-
- self.timer = wx.Timer(self)
- self.Bind(wx.EVT_TIMER, self.onTimer)
- self.timer.Start(100)
-
- def onLoadFile(self, evt):
- dlg = wx.FileDialog(self, message="Choose a media file",
- defaultDir=os.getcwd(), defaultFile="",
- style=wx.OPEN | wx.CHANGE_DIR )
- if dlg.ShowModal() == wx.ID_OK:
- path = dlg.GetPath()
- self.doLoadFile(path)
- dlg.Destroy()
-
- def doLoadFile(self, path):
- if not self.mc.Load(path):
- wx.MessageBox("Unable to load %s: Unsupported format?" % path, "ERROR", wx.ICON_ERROR | wx.OK)
- else:
- folder, filename = os.path.split(path)
- self.st_file.SetLabel('%s' % filename)
- self.mc.SetBestFittingSize()
- self.GetSizer().Layout()
- self.slider.SetRange(0, self.mc.Length())
- self.mc.Play()
-
- def onPlay(self, evt):
- self.mc.Play()
-
- def onPause(self, evt):
- self.mc.Pause()
-
- def onStop(self, evt):
- self.mc.Stop()
-
- def onSeek(self, evt):
- offset = self.slider.GetValue()
- self.mc.Seek(offset)
-
- def onTimer(self, evt):
- offset = self.mc.Tell()
- self.slider.SetValue(offset)
- self.st_size.SetLabel('size: %s ms' % self.mc.Length())
- self.st_len.SetLabel('( %d seconds )' % (self.mc.Length()/1000))
- self.st_pos.SetLabel('position: %d ms' % offset)
-
-
- app = wx.PySimpleApp()
-
- frame = wx.Frame(None, -1, "play audio and video files", size = (320, 350))
-
- Panel1(frame, -1)
- frame.Show(1)
- app.MainLoop()