Initial commit of BGMusic

This commit is contained in:
Media
2025-06-28 01:08:57 -05:00
commit f198e7fefa
1679 changed files with 282367 additions and 0 deletions
+96
View File
@@ -0,0 +1,96 @@
import os
import subprocess
import paho.mqtt.client as mqtt # type: ignore
import time
import threading
current_process = None
def on_message(client, userdata, message):
global current_process
global playlists
topic = message.topic
payload = message.payload.decode().strip()
print(f"📡 MQTT: {topic} → {payload}")
if topic == "playlist/set":
playlist_name = payload
playlist_path = playlists.get(playlist_name)
if not playlist_path or not os.path.exists(playlist_path):
print(f"❌ Playlist '{playlist_name}' not found.")
return
if current_process and current_process.poll() is None:
print("🛑 Stopping current playlist...")
current_process.terminate()
current_process.wait()
print(f"🎧 Starting playlist: {playlist_name}")
current_process = subprocess.Popen(["python3", "Playlist.py", playlist_path],stdin=subprocess.PIPE)
#elif topic == "player/toggle": NOTE: Left in for the "Just in case" scenario.
#if current_process and current_process.poll() is None: NOTE: Audio "pausing" is handled by AHM fading to 0%
#print(f"⏯ Sending toggle: {payload}")
#current_process.stdin.write((payload + "\n").encode())
#current_process.stdin.flush()
#return
elif topic == "player/skip":
if current_process and current_process.poll() is None:
print("⏩ Skipping current song...")
current_process.stdin.write(b"skip\n")
current_process.stdin.flush()
return
client = mqtt.Client()
client.on_message = on_message
client.connect("localhost", 1883)
client.subscribe("playlist/set")
#client.subscribe("player/toggle") NOTE: Play/pause toggle
client.subscribe("player/skip")
client.loop_start()
playlist_dir = "/home/media/Scripts/BGMusic/Playlists"
playlists = {
"Worship": os.path.join(playlist_dir, "Worship.txt"),
"Praise": os.path.join(playlist_dir, "Praise.txt"),
"Instrumental": os.path.join(playlist_dir, "Instrumental.txt")
}
selected = "Worship" #default playlist
playlist_path = playlists.get(selected)
if not playlist_path or not os.path.exists(playlist_path):
print("Playlist not found!")
exit(1)
current_process = subprocess.Popen(["python3","Playlist.py",playlist_path], stdin=subprocess.PIPE)
def listen_for_local_q():
while True:
key = input().strip()
if key.lower() == "q":
if current_process and current_process.poll() is None:
print("🛑 Sending 'q' to playlist subprocess")
current_process.stdin.write(b"q\n")
current_process.stdin.flush()
threading.Thread(target=listen_for_local_q, daemon=True).start()
try:
while True:
if current_process and current_process.poll() is not None:
print("🛑 Playlist process ended. Exiting controller.")
break
time.sleep(0.1)
except KeyboardInterrupt:
print("\n👋 Exiting on Ctrl+C.")