2014-05-15 98 views
-1

关于在Python中控制Windows媒体播放器的任何想法?我发现在网上运行良好,但没有音频播放下面的代码。我正在使用win7 64位机器自动化Windows媒体播放器

# this program will play MP3, WMA, MID, WAV files via the WindowsMediaPlayer 
from win32com.client import Dispatch 
mp = Dispatch("WMPlayer.OCX") 
#tune = mp.newMedia("./SleepAway.mp3") 
tune = mp.newMedia("./plays.wav") 
mp.currentPlaylist.appendItem(tune) 
mp.controls.play() 
raw_input("Press Enter to stop playing") 
mp.controls.stop() 
+0

WMP的音量设置? –

+1

这可能与[本周遇到的问题]有关(https://stackoverflow.com/questions/25157138)。关键是要确保您在控制线程中抽取Windows消息。 raw_input不这样做。 –

+0

我对于一个有相同的问题,使用Windows 7和python以编程方式播放声音...如果你想要看到我的问题,但它永远不会解决。看起来你可以很好地使用os.startfile,也许可以看看。这里是我的问题在http://stackoverflow.com/questions/20696948/how-to-do-text-to-speech-with-python-on-a-toshiba-laptop-and-windows-7 –

回答

1

正如我在评论中提到的,我有一个相同的问题。我试过a ridiculous number of different approaches。他们都没有真正的工作,所以我被困在使用os.startfile来打开Windows媒体播放器播放我的声音文件。但是,就在今天,我有一个想法,导致了另一种解决方案。这有点破解,但它的工作原理。从技术上讲,我仍然使用这种方法打开Windows媒体播放器,但是我使用子进程来进行操作,因此我可以更好地控制该进程所允许的进程来抑制窗口。这使它看起来像没有辅助应用程序一样播放。为什么我必须做些奇怪的事情才能得到我不知道的简单结果,但这是唯一奏效的东西。这是我的代码,如果你想。

import subprocess 
import threading 

def windowsmedia(filename): 
    startupinfo = subprocess.STARTUPINFO() 
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW 
    a = subprocess.call('C:\\Program Files (x86)\\Windows Media Player\\wmplayer.exe /play /close "C:/Users/Student/Documents/notes/file.mp3"',startupinfo=startupinfo) 

def startmp3(filename): 
    mythread = threading.Thread(target=windowsmedia,args=[filename]) 
    mythread.start() 
    time.sleep(15) #You might want to extend this... I just give it 15 seconds to complete before killing the process. It shouldn't be too hard to read the exact length from the file and wait that, or add an interrupt, but that was somewhat unnecessary for my purposes. 
    pkill("wmplayer") #This is a function of my own but it basically just kills the process. It shouldn't be too hard to reproduce. 

同样它确实令人遗憾的是我必须做的事情如此怪异的只是播放声音,但只要你描述它,这是同样的问题,我希望这有助于。

0

想到了,它可能会帮助其他仍然面临这个问题的人。 您只需在Play()后调用PlayItem()API。

from win32com.client import Dispatch 
from time import sleep 

mp = Dispatch("WMPlayer.OCX") 
tune = mp.newMedia("./plays.wav") 
mp.currentPlaylist.appendItem(tune) 
mp.controls.play() 
sleep(1) 
mp.controls.playItem(tune) 
raw_input("Press Enter to stop playing") 
mp.controls.stop()