#!/bin/bash # # Script called by a2dp-agent to switch the mpd queue to Bluetooth A2DP and # restore and continue playing when the Bluetooth connection is terminated. # # Version 1.0 03-11-2025 # Read configuration . /etc/default/bluez-alsa # Message logging. info_log() { # If possible, log events in /var/log/messages: if [ -f /var/run/syslogd.pid ] && [ -x /usr/bin/logger ]; then /usr/bin/logger -t "a2dp-hook" --id="$$" "$*" else printf "%s: %s\\n" "a2dp-hook" "$*" fi } start() { info_log "Start A2DP sink" # First, create a new current playlist. if [ -f /var/mpd/playlists/QueueFile.m3u ]; then mpc rm QueueFile fi mpc save QueueFile info_log "Save current playlist in QueueFile.m3u" # Save current song in this playlist. POSITION=$(mpc status | head -2 | tail -1 | awk -F ' ' '{print $2}' | awk -F '#' '{print $2}' | awk -F '/' '{print $1}') echo -n $POSITION > /var/mpd/a2dp-position info_log "Playlist position $POSITION" # Create a new playlist if [ -f /var/mpd/playlists/Bluez.m3u ]; then mpc rm Bluez fi mpc addplaylist Bluez MPD_Specials/BatchComplete.mp3 mpc addplaylist Bluez ${ALOOP} info_log "Created new playlist Bluez.m3u" # Activate the new playlist mpc clear >/dev/null mpc load Bluez >/dev/null mpc play >/dev/null info_log "Started playlist Bluez.m3u" } stop() { info_log "Stop A2DP sink" mpc clear >/dev/null mpc load QueueFile >/dev/null mpc play $(cat /var/mpd/a2dp-position) >/dev/null info_log "Loaded playlist QueueFile.m3u, start play $(cat /var/mpd/a2dp-position)" } case "$1" in start) start ;; stop) stop ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0