以最少的依赖在 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中文网其他相关文章!