自动化 windows 媒体播放器 [英] Automating windows media player

查看:37
本文介绍了自动化 windows 媒体播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在 Python 中控制 windows 媒体播放器的任何想法?我在网上发现以下代码运行良好但没有播放音频.我使用的是 win7 64 位机器

Any ideas about controling windows media player in Python? I found the following code on the net which runs fine but no audio is played. am using win7 64 bit machine

# 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()

推荐答案

正如我在评论中提到的,我遇到了同样的问题.我试过 大量不同的方法.它们都没有真正起作用,所以我被困在使用 os.startfile 打开 windows 媒体播放器来播放我的声音文件.然而,就在今天,我有了一个想法,导致了另一种解决方案.这有点hack-ish,但它有效.从技术上讲,我仍然使用这种方法打开 windows 媒体播放器,但是我使用子进程来打开它,因此我可以对它允许的进程使用更大的控制来抑制窗口.这使得它看起来像是在没有辅助应用程序的情况下播放.为什么我必须做一些如此奇怪的事情才能得到一个我不知道的简单结果,但这是唯一有效的方法.如果你愿意,这是我的代码.

As I have mentioned in comments, I have had an identical problem. I tried a ridiculous number of different approaches. None of them really worked, so I was stuck using os.startfile to open windows media player to play my sound files. However, just today I had an idea which has led to an alternative solution. It is a bit hack-ish, but it works. Technically I still open windows media player with this approach, but I do so using subprocess, and thus I can use the greater control over the process allowed by that to supress the window. This makes it seem like it plays without a secondary application. Why I had to do something so bizarre to get a simple result I have no idea, but it is the only thing that worked. Here is my code for it if you want.

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.

再次遗憾的是,我不得不为了播放声音而做一些如此奇怪的事情,但就您所描述的而言,这是同一个问题,我希望这会有所帮助.

Again it is truly regrettable that I had to do something so weird for just playing a sound but as far as you have described it this is the same issue and I hope this helps.

这篇关于自动化 windows 媒体播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆