以最少的依賴在 Python 中播放音訊檔案
Python 缺乏用於開箱即用地播放聲音檔案的跨平台模組。對於Windows 系統,winsound 提供了一個簡單的解決方案:
<code class="python">import winsound winsound.PlaySound('sound.wav', winsound.SND_FILENAME)</code>
對於Linux,ossaudiodev 提供了更大的靈活性:
<code class="python">from wave import open as waveOpen from ossaudiodev import open as ossOpen s = waveOpen('tada.wav','rb') (nc,sw,fr,nf,comptype, compname) = s.getparams( ) dsp = ossOpen('/dev/dsp','w') try: from ossaudiodev import AFMT_S16_NE except ImportError: from sys import byteorder if byteorder == "little": AFMT_S16_NE = ossaudiodev.AFMT_S16_LE else: AFMT_S16_NE = ossaudiodev.AFMT_S16_BE dsp.setparameters(AFMT_S16_NE, nc, fr) data = s.readframes(nf) s.close() dsp.write(data) dsp.close()</code>
(ossaudiodev 的來源:Bill Dandreta https://mail.python .org/pipermail/python-list/2004-October/288905.html)
以上是如何在 Python 中以最少的依賴播放音訊檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!