added soundboard
This commit is contained in:
@@ -222,6 +222,14 @@ def control_page():
|
||||
return render_template('control.html', config=current_config)
|
||||
|
||||
|
||||
@app.route('/soundboard')
|
||||
def soundboard():
|
||||
if current_config is None or 'sounds' not in current_config:
|
||||
return redirect(url_for('index'))
|
||||
|
||||
sounds = current_config.get('sounds', [])
|
||||
return render_template('soundboard.html', sounds=sounds, config=current_config)
|
||||
|
||||
# ── Admin ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@app.route('/admin')
|
||||
@@ -513,6 +521,54 @@ def api_reconnect():
|
||||
return jsonify({"success": False, "message": str(e)}), 500
|
||||
|
||||
|
||||
import pygame
|
||||
import threading
|
||||
|
||||
# pygame einmalig initialisieren (am besten global)
|
||||
pygame.mixer.init()
|
||||
|
||||
@app.route('/api/play_sound', methods=['POST'])
|
||||
def api_play_sound():
|
||||
try:
|
||||
data = request.get_json()
|
||||
sound_id = data.get('sound_id')
|
||||
|
||||
if not sound_id:
|
||||
return jsonify({"success": False, "message": "Kein sound_id angegeben"}), 400
|
||||
|
||||
# Sound aus aktueller Config suchen
|
||||
if current_config is None or 'sounds' not in current_config:
|
||||
return jsonify({"success": False, "message": "Keine Sounds in der aktuellen Konfiguration"}), 400
|
||||
|
||||
sound_entry = next((s for s in current_config['sounds'] if s['id'] == sound_id), None)
|
||||
if not sound_entry:
|
||||
return jsonify({"success": False, "message": f"Sound mit ID '{sound_id}' nicht gefunden"}), 404
|
||||
|
||||
file_path = os.path.join('sounds', sound_entry['file'])
|
||||
if not os.path.exists(file_path):
|
||||
logger.error(f"Sound-Datei nicht gefunden: {file_path}")
|
||||
return jsonify({"success": False, "message": "Sound-Datei nicht gefunden"}), 404
|
||||
|
||||
# Sound in separatem Thread abspielen (blockiert nicht)
|
||||
def play():
|
||||
try:
|
||||
pygame.mixer.music.load(file_path)
|
||||
pygame.mixer.music.play()
|
||||
while pygame.mixer.music.get_busy():
|
||||
time.sleep(0.1)
|
||||
except Exception as e:
|
||||
logger.error(f"Fehler beim Abspielen von {file_path}: {e}")
|
||||
|
||||
threading.Thread(target=play, daemon=True).start()
|
||||
|
||||
logger.info(f"Spiele Sound: {sound_entry['name']} ({sound_id})")
|
||||
return jsonify({"success": True, "message": f"Spiele: {sound_entry['name']}"})
|
||||
|
||||
except Exception as e:
|
||||
logger.exception("Play-Sound-Fehler")
|
||||
return jsonify({"success": False, "message": str(e)}), 500
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000, debug=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user