MPVMediaControl icon indicating copy to clipboard operation
MPVMediaControl copied to clipboard

How to make MPVMediaControl.exe process to close together with mpv?

Open KonoVitoDa opened this issue 1 year ago • 3 comments

After closing mpv, the process continues to run in the taskbar indefinitely, and this behavior is especially annoying because it makes previously opened files unable to be moved/deleted as Windows claims they are currently running.

KonoVitoDa avatar Feb 19 '24 00:02 KonoVitoDa

Yeah this is the reason I stick with the now abandoned MPV-SMTC. It opens and closes with MPV, it's also a silent subprocess so it never needs to be in the task bar to begin with.

xathian avatar Aug 02 '24 04:08 xathian

Yeah this is the reason I stick with the now abandoned MPV-SMTC. It opens and closes with MPV, it's also a silent subprocess so it never needs to be in the task bar to begin with.

It doesn't seem to work on Windows 11 :(

KonoVitoDa avatar Aug 07 '24 21:08 KonoVitoDa

Also interested in this for the same reason. Constantly getting "cannot delete folder in use" errors from Windows while deleting and re-organizing videos because this was still open in the System Tray even though all instances of mpv were closed.

Looks like the ExitIfNoControllers() in notify_media.lua gets registered to run when mpv is closed. It does an IPC send that's received in PipeServer.cs. Considering the other IPC calls seem to be working correctly, I assume it's getting to this case successfully.

Not at all familiar with C# or relevant Windows APIs but the Exit() function that's supposed to be called eventually is also used here in Program.cs for the tray icon's context menu, which works as you'd expect. Therefore, I also assume the Exit() function itself is also working correctly, if called.

The above mentioned setQuit case:

  1. Checks if the "quit" parameter is true (should be, based on the IPC call in notify_media.lua).
  2. Checks that Program.AppContext, controller, and controller.File.Path aren't null (seems like that should be true but idk?).
  3. Sets the controller to stop if it isn't already (probably fine?).
  4. Calls RemoveController(pid) (no idea if it's successfully doing so).
  5. Calls ExitIfNoControllers() which should then do some async stuff to call Exit(), but only if _controllers.Count == 0, which should be true if the previous step worked.

So maybe:

  • Registering with mpv to run a lua function on shutdown isn't working?
  • IPC send/receive failure?
  • One of those variables is null?
  • RemoveController(pid) isn't successfully doing so... maybe the pid isn't matching the one used when the MediaController was created?
  • Something with the async or await in ExitIfNoControllers() that calls Exit()? Seems correct to my untrained eye.

Wish I was set up and qualified to help, proooobably not a difficult problem to debug (knock on wood).

conradsrc avatar Aug 24 '24 01:08 conradsrc

A quick fix for this without support for multiple mpv instances:

function on_quit()
    if shot_path then
        os.remove(shot_path)
    end
    write_to_socket("^[setQuit](pid=" .. pid .. ")(quit=true)(socket_name=" .. mpv_socket_name .. ")$")
    
    -- Force kill MPVMediaControl.exe
    mp.command_native({
        name = "subprocess",
        playback_only = false,
        args = {"taskkill", "/F", "/IM", "MPVMediaControl.exe"},
    })
end

Toastbroti avatar Aug 31 '25 18:08 Toastbroti