1

I run MediaPlayerClassic, as a minimized window in the taskbar (see here and the other answer too), with:

import subprocess, win32con, win32gui, win32process, win32api

info = subprocess.STARTUPINFO()
info.dwFlags = subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = win32con.SW_MINIMIZE
app = subprocess.Popen(r'c:\path\to\mpc-h64.exe "d:\mp3\song.mp3"', startupinfo=info)

It works. But sometimes, I would like to replace the currently-playing-song by another one, and I'm sending the same previous code (with another mp3).

Since MediaPlayerClassic allows only 1 instance, re-running the previous code does replace the currently-playing-song by the new one indeed, and this is what I want, so it works too. Except that the new window is no more minimized. This is probably because no new process is started: it reuses the previously existing process, and MediaPlayerClassic "opens the window" when it receives a new MP3 to play (?).

Adding:

time.sleep(0.2)  # required, if not the next action happens before the MP3 changes

def callback(hwnd, hwnds):
    text = win32gui.GetWindowText(hwnd)
    if win32gui.GetClassName(hwnd) == 'MediaPlayerClassicW':
        win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE)
    return True

win32gui.EnumWindows(callback, [])

nearly solves it, except that the window quickly flashes and then minimizes.

Question: how to relaunch a .exe already started like MediaPlayerClassic and keep it minimized? Should something else than subprocess.Popen be used here?

Basj
  • 41,386
  • 99
  • 383
  • 673
  • MPC is doing this on purpose. – Anders Mar 19 '19 at 11:28
  • @Anders any idea how to avoid that MPC does this? – Basj Mar 19 '19 at 11:34
  • You should use ShellExecute() to play media files. – Michael Chourdakis Mar 19 '19 at 12:54
  • @Michael, no I would like to keep MPC, because I want to use MPC's user interface sometimes (by clicking it on the taskbar and open it). – Basj Mar 19 '19 at 13:05
  • Then kill the old process with TerminateProcess() and start a new one. In any case, making assumptions on a 3rd party program behaviour will create issues in future updates unless documented. – Michael Chourdakis Mar 19 '19 at 14:50
  • @Michael Yes, I thought about this, but I think it will introduce a bigger delay to the current solution I use (maybe 1 or 2 seconds for the app to close and restart). – Basj Mar 19 '19 at 15:12
  • using `FindWindow("WINDOWSPROJECT9",None)` instead of retrieving all windows to reduce the delay? – Drake Wu Mar 20 '19 at 08:17
  • @DrakeWu-MSFT Thank you for your tip. The `EnumWindows` loop is in fact very short, so that's not really the problem ; if I reduce the delay too much (`time.sleep(0.2)`), the next action happens before the MP3 has changed. – Basj Mar 20 '19 at 08:44

0 Answers0