feat(audio): beheer Pi volume persistentie en Docker compatibiliteit

Het volume-instellingssysteem is geüpdatet om persistentie te ondersteunen, zelfs in Docker-containers. Het configureren van het volume met `amixer` is nu conditioneel, en als er in een Docker-omgeving wordt gewerkt, wordt alleen de instelling opgeslagen zonder directe toepassing. Een nieuw opstartscript `pi-volume-startup.sh` is toegevoegd om het HDMI-volume van de Pi automatisch in te stellen bij het opstarten op basis van de opgeslagen instellingen. Dit verbetert de gebruikerservaring en het onderhoud van het volume-instellingensysteem.
This commit is contained in:
filoor 2025-05-28 20:32:44 +02:00
parent 40e3bcc527
commit 1a91c9f577
2 changed files with 55 additions and 24 deletions

View File

@ -763,33 +763,41 @@ def set_pi_volume():
if not isinstance(volume, int) or volume < 0 or volume > 100: if not isinstance(volume, int) or volume < 0 or volume > 100:
return jsonify({'success': False, 'error': 'Volume moet tussen 0 en 100 zijn'}), 400 return jsonify({'success': False, 'error': 'Volume moet tussen 0 en 100 zijn'}), 400
# Stel Pi volume in via amixer # Sla volume op in settings voor persistentie
try: settings = load_settings()
# Stel PCM volume in (HDMI audio) settings['pi_hdmi_volume'] = volume
subprocess.run(['amixer', 'set', 'PCM', f'{volume}%'],
check=True, capture_output=True, text=True) with open(SETTINGS_PATH, 'w') as f:
json.dump(settings, f, indent=2)
# Sla volume op in settings voor persistentie
settings = load_settings() # Check of we in een Docker container draaien
settings['pi_hdmi_volume'] = volume if os.path.exists('/.dockerenv'):
print(f"🔊 Pi HDMI volume opgeslagen: {volume}% (Docker container)")
with open(SETTINGS_PATH, 'w') as f:
json.dump(settings, f, indent=2)
print(f"🔊 Pi HDMI volume ingesteld op {volume}%")
return jsonify({ return jsonify({
'success': True, 'success': True,
'message': f'Pi HDMI volume ingesteld op {volume}%', 'message': f'Pi HDMI volume opgeslagen op {volume}%. Volume wordt toegepast bij browser herstart.',
'volume': volume 'volume': volume,
'info': 'Docker container - volume opgeslagen voor browser audio'
}) })
else:
except subprocess.CalledProcessError as e: # Direct op Pi - probeer amixer
print(f"❌ Amixer fout: {e}") try:
return jsonify({ subprocess.run(['amixer', 'set', 'PCM', f'{volume}%'],
'success': False, check=True, capture_output=True, text=True)
'error': f'Kon volume niet instellen: {e.stderr if e.stderr else str(e)}' print(f"🔊 Pi HDMI volume direct ingesteld op {volume}%")
}), 500 return jsonify({
'success': True,
'message': f'Pi HDMI volume ingesteld op {volume}%',
'volume': volume
})
except (subprocess.CalledProcessError, FileNotFoundError) as e:
print(f"⚠️ Amixer fout: {e}")
return jsonify({
'success': True,
'message': f'Volume opgeslagen op {volume}%. Amixer niet beschikbaar.',
'volume': volume,
'warning': str(e)
})
except Exception as e: except Exception as e:
print(f"❌ Volume API fout: {e}") print(f"❌ Volume API fout: {e}")

23
pi-volume-startup.sh Normal file
View File

@ -0,0 +1,23 @@
#!/bin/bash
# Script om Pi HDMI volume in te stellen bij opstarten
SETTINGS_FILE="/home/pi/adhan/adhan-webapp/settings.json"
if [ -f "$SETTINGS_FILE" ]; then
# Haal volume uit settings.json
VOLUME=$(python3 -c "
import json
try:
with open('$SETTINGS_FILE') as f:
settings = json.load(f)
print(settings.get('pi_hdmi_volume', 70))
except:
print(70)
")
echo "🔊 Pi HDMI volume instellen op ${VOLUME}%..."
amixer set PCM ${VOLUME}% 2>/dev/null || echo "⚠️ Amixer niet beschikbaar"
else
echo "⚠️ Settings bestand niet gevonden, gebruik standaard volume 70%"
amixer set PCM 70% 2>/dev/null || echo "⚠️ Amixer niet beschikbaar"
fi