I'm asking for any solution (algorithm, program installation ...) to get music stopping when we remove headphones,
Thank you !
2 Answers
It happens by default , as ubuntu 14.04 have different profiles for your headphone and your speaker. Just mute your speaker first and then insert your headphones, it will activate headphone profile and you can listen music, the moment you remove your headphone it will go back to speaker profile which was initially muted, so it will be muted.
Actually, it has a 1sec delay. As I couldn't find any system event that triggered with the remove of headphones plug, so I used 1sec delay loop.
Check for sink ID and port names using:
pactl list sinksThen change them in this script that monitor port state then send MPRIS pause command:
#!/bin/bash sink_id=1 port_name_headset=analog-output-headphones port_name_speaker=analog-output-speaker while sleep 1 ; do cur_port=`pactl list sinks | awk '/Active Port:/ { print $3 }' | awk NR==$(( $sink_id + 1 ))` echo -n -e "\n was $prev_port now $cur_port" if [[ "$prev_port" = "$port_name_headset" && "$cur_port" = "$port_name_speaker" ]] then echo " << Pause >>" gdbus call --session \ --dest org.mpris.MediaPlayer2.vlc.instance$(pgrep vlc) \ --object-path /org/mpris/MediaPlayer2 \ --method org.mpris.MediaPlayer2.Player.Pause fi prev_port=$cur_port done
Note:
This is only for VLC but it could be modified to cover other MPRIS players. See How do I stop xscreensaver during movies play?
Reference: MPRIS D-Bus Interface Specification
If you mean just to mute sound, use this command (just change the
id):pactl set-sink-mute id 1Reference: Volume via command line in 12.04