Automating windows media player

后端 未结 3 754
-上瘾入骨i
-上瘾入骨i 2021-01-29 06:10

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

<         


        
相关标签:
3条回答
  • 2021-01-29 06:27

    It helps me to use Windows Media COM. When I tried it, I need to make 2 small modification to make it working in Python Flask.

    1. CoInitialize to make it single thread i.e. pythoncom.CoInitialize() and pythoncom.CoUninitialize()
    2. PumpWaitMessage to keep MediaPlayer working i.e while mp.PlayState != 1: pythoncom.PumpWaitingMessages()
    0 讨论(0)
  • 2021-01-29 06:35

    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.

    0 讨论(0)
  • 2021-01-29 06:42

    Thought, it might help others who are still facing this issue. All you had to do is to call PlayItem() API after Play().

    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()
    
    0 讨论(0)
提交回复
热议问题