Automating windows media player - python

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

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.

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

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.
CoInitialize to make it single thread i.e. pythoncom.CoInitialize() and pythoncom.CoUninitialize()
PumpWaitMessage to keep MediaPlayer working i.e while mp.PlayState != 1: pythoncom.PumpWaitingMessages()

Related

more info or docs for pyvda, or a better module:

import time
import subprocess
from pyvda import AppView, get_apps_by_z_order, VirtualDesktop, get_virtual_desktops
#clockify
subprocess.Popen("C:/Program Files/Clockify/ClockifyWindows.exe")
current_window = AppView.current()
target_desktop = VirtualDesktop(7)
current_window.move(target_desktop)
VirtualDesktop(7).go()
trying to load specific programs to specific desktops in windows 11; so, my "clockify" app loads on the desktop "clock" and blender loads on the blend desktop so on so on.
cannot find any docs for this mofo, but pieced together what's above.
it would be awesome to:
a) check if a virtual desktop exist, and create it, incase of accidental deletion.
but really what I need is a way to specify the "current window". right now (i'm assuming python sees the that as whatever window is active as the current window); I have to put the sleep timer inbeteen loading programs, which is fine.
b) need a way to specify what window as active, then i can tie that to the appropriate desktop and maybe load them all at once to save time
#still a newb and no formal training. and alot of aconyms confuse me, and obvious assumptions may not be obvious to me.
cheers,
-mda

How to stop an external program started by python on linux

I previously asked how to stop a program and was told/helped, however I neglected to mention the program was external. I wish to press a macro/key and have a external program run, and when pushed again it is stopped/halted all from this code. Eventually I hope to have many macro-keys toggling different programs on/start and off/stop.
I have searched and come up with a post from 2016 I think, I have adapted the code slightly, I have it working(starting) an external program but not stopping it. Code is run on Linux and written in python, it follows this, the EXTERNAL program is not killed or stopped.
What bug have I introduced, how to do it.
#!/usr/bin/env python
# Toggler for macro key -|- many keys = one toggler program
import os
import sys
import time
import subprocess
def startp(program):
return subprocess.Popen(program)
def exitp(p):
#os.close(program)#requires integer
p.terminate()
program = startp("firefox")
time.sleep(5)
# program = 'firefox'
exitp(program)
Perhaps someone can help find the bug in my program that I have adapted.
skeptic

Sending keyboard input to windows lock screen

As it sounds I wanted to create my own kinda smart assistant that can actually unlock my pc (Cortana can't and I could not find any good solution for this).
for now, I have tried multiple ways including messing with winlogon.exe and MSGINA.dll. After about 3 hours of search, I haven't found anything that could actually unlock my pc, and I saw some programs that said that they have succeeded in unlocking their pc while messing around with this files but in reality, it just prevents the locking mechanism and it is not my intention.
right now I have seen some videos about using pyautogui and keyboard to control keyboard and mouse movement and I wrote a small script that can "unfold" the windows lock screen cover (where the time and date is displayed) but nothing more than that (it is not working at all)
import keyboard
import time
for i in range(10):
print 10 - i
time.sleep(1)
keyboard.send('enter')
time.sleep(2)
keyboard.write("password1")
keyboard.send("enter")
also tried keyboard.press_and_release and it doesn't do anything.
how can I send any keyboard input to the lock screen?
and if it is not possible, do you have any other suggestions?
EDIT: I am Working on windows 10 build 1803 and python 2.7

control vlc with python

I would need two things in vlc that I want to control from a Python script. First is to open a network stream, add the stream address as a url in it and play it. (This might be skippable)
The second thing is to take a snapshot at a specific time and use that picture. As I saw the different kind of libraries and modules, they can only things like play, pause, rewind a video.
Can anybody help me with this one?
Thanks in advance!
You can use os.chdir(path) and os.system(command)
Find where your vlc executable (.exe) is, and store the path in a variable.
Then you can use os.system to execute a given command.
Here you see a list of possible command-line options for VLC
Example code:
import os
vlc_path = "C:\path\to\vlc"
net_stream = "http://host[:port]/file" # You can use other protocols too
os.chdir(vlc_path)
os.system(f"vlc {net_stream}")

Python detect sleep mode in windows10

I want to find out if I can launch a function or a method in a python program right before the computer sleep, something that goes along the line of
def f():
if about_to_sleep:
do_stuff
else:
do_some_other_stuff
that can work in windows 10
If you didn't mind writing a small C++ application to run in the background, it could call your python script when it detects the computer going into sleep.
Take a look at:
Qt detect when computer goes into sleep?
Detecting computer/program shutdown in Python?
This was a question similar to yours, but might still be relevant.

Categories

Resources