How to create shortcut to stop and minimize media player

Its been a long time with me searching for this throughout the internet
but cant find any better solution

lemme get to the point
previously when i was in window i use to play movies/series using KM-PLAYER

in this player there used to be one hotkeys ESC which pauses the playing media automatically and minimizes it to the taskbar automatically just with one button

i tried many players but none have that functionality !!

anyone here can help me in that?
to make that feature work on VLC player or HARUNA MEDIA PLAYER

Provide some information on your environment first? Are you using Xorg or Wayland? Without these info, it’s impossible to give a precise solution.

For this behavior, you are going to have to define a custom keybinding for your desktop environment/window manager.

Assuming that you’re on Xorg, you can use xdotool to minimize your windows. To pause VLC, you need to install playerctl, which allows you to control VLC from inside a shell script.

Once you have all the necessary tools, simply put them all in a shell script.

#!/bin/sh
# This script minimizes the currently active window and pauses VLC
# Replace $(xdotool getactivewindow) with something else if you want more specific funtionality
# E.g: you can also use xdotool to search the window's WM_CLASS attribute
xdotool minimize $(xdotool getactivewindow)
playerctl --player=vlc pause

Make this shell script executable with chmod. After that, add a keybinding in your desktop environment/window manager to launch this script every time the defined key is pressed.

playerctl is in Arch’s extra repo. You can install it with pacman:

sudo pacman -Syu playerctl

References:
Documentation for playerctl

how to know wheather its wayland or xorg

echo $XDG_SESSION_TYPE

it showed X11

Then you’re on Xorg. You can follow the instructions I described above if you want.

If you want more accurate behavior, you can modify the script like this:

#!/bin/sh
winclass=$(xdotool getactivewindow getwindowclassname)
if [ "$winclass" = "Vlc" ]
then
     # only takes effect if the current active window is vlc
     xdotool minimize $(xdotool getactivewindow)
     playerctl --player=vlc pause
fi

This will trigger the minimize + pause behavior only if your current active window is vlc.

You can use smplayer, check the options/preferences/general “pause when minimized”, works great here!

3 Likes

Nice. Even better if there’s a player with this function baked in. I’m all for the good old mpv myself.

2 Likes

can i get the methods more clearly
i am new in these feild soo
i really dont know exactly how to do it

vlc also have this option but

i want the feature to be like :
when pressing ESC it should automatically pause the media playing and minimize it to the taskbar or the tray

Install smplayer, then go to menu / options / preferences / general, then check the box “Pause when minimized”. Then when you minimize the window it pauses whatever is playing!

You can also menu / options “Show icon in system tray” so even if you “close” the window it stays minimized in tray

1 Like

Sorry, I am out of further options…

Step 1: Go to your terminal.

Step 2: Inside the terminal, type cd and press enter. This will change to your home directory.

Step 3: In the same terminal, run this command touch pause-and-minimize-vlc.sh. This will create an empty text file in your home directory.

Step 4: Open pause-and-minimize-vlc.sh with a text editor (kate, mousepad, or whichever text editor you prefer).

Step 5: Paste the following into pause-and-minimize-vlc.sh

#!/bin/sh
winclass=$(xdotool getactivewindow getwindowclassname)
if [ "$winclass" = "Vlc" ]
then
     # only takes effect if the current active window is vlc
     xdotool minimize $(xdotool getactivewindow)
     playerctl --player=vlc pause
fi

Step 6: Save the file.
Step 7: Go back to your terminal and type

chmod +x ~/pause-and-minimize-vlc.sh

This will make the shell script executable.

Step 8: Add a keyboard shortcut to run this shell script every time the shortcut is pressed. How you do this depends on the desktop environment you’re using. If you are using XFCE or KDE, there are probably graphical tools to do this. I don’t recommend binding the ESC key for this function, since the ESC key has its own uses. Choose something like Win + M or something.

3 Likes

If you are certain that VLC does not support this feature (by looking at their documentation), then you have no choice but to implement it yourself by writing a shell script.

can you teach me to do this

I already did.

Also, just as a reminder. You need to make sure that xdotool and playerctl are both installed on your system for that shell script to work.

adding a shortcut for VLC only ?
can you explain it ?

i installed it after applying all those
will it affect it ?