os.startfile is going to just open the file with the default associated program, akin to what happens when you double click it. There is a couple things you can try.
You can try to create a .m3u
playlist file. Iterate over the files and programmatically create it. I recommend looking up the exact details, but they will look something like this:
#EXTM3U
#EXTINF:1,Artist - Song title 1
D:\path\to\file\01 Song.mp3
#EXTINF:1,Artist - Song title 2
D:\path\to\file\02 Song.mp3
This file also has an associated program, so you can then open it with os.startfile
. The number after EXTINF
is the length of the track in seconds. It does not appear strictly necessary to have this correct as players I tried would display the correct time. WMP did not seem to like a value of 0, though, so a value of at least 1 should probably be used. The path can either be absolute or relative.
There is also .m3u8 which is the UTF-8 version if you need that.
The other option you have is to try to find the command to execute to add to the WMP playlist. This appears to be the commands available for WMP. You maybe want that /Task NowPlaying
one? They do not have a full example of using it and not clear if you can mix that with opening a file.
Once you know the task and get that working from just the command line, you can try to execute it with something from the subprocess module. Depending on your python version:
import shlex
import subprocess
from subprocess import PIPE
command = 'wmplayer "c:\filename.wma"'
subprocess.run(shlex.split(command), stdout=PIPE, stderr=PIPE)
Don't quote me on those quotes as I do not exactly have the environment to run this, but should be something close to that. You may or may not want the stdout=PIPE, stderr=PIPE
. That will capture the out and err output so you could print them if you wanted but can also be troublesome if the output is large.