如何在后台播放声音? (用于Windows的python 3.5.2)

时间:2016-11-14 01:36:54

标签: python audio

这是我的代码:

import subprocess
import os

path_to_notepad = 'C:\\Program Files (x86)\\Windows Media Player\\wmplayer.exe'
path_to_file = 'C:\\Users\iceki_000\Desktop\\ayy.mp3'

subprocess.call([path_to_notepad, path_to_file])

到目前为止,我已经使用Windows Media Player打开了一个音频文件。是否可以将WMP作为后台进程启动,因此声音会播放,但窗口不会被显示?

1 个答案:

答案 0 :(得分:0)

如果你安装 python for windows ,那么你应该有winsound模块。用法很简单,它有各种标志可以设置何时,如何播放歌曲并停止播放。没有像我在Linux上那样测试它

              import winsound
              #to play the music
              winsound.PlaySound("filename",  winsound.SND_ALIAS)

              #to stop the music 
              winsound.PlaySound(None, winsound.SND_ALIAS)

              #to play music asynchronously
              winsound.PlaySound("filename",  winsound.SND_ALIAS | winsound.SND_ASYNC) 

参考: - https://docs.python.org/2/library/winsound.html#module-winsound

stackoverflow: - Python: Playing a music in the background?