02-ago: [proyecto] vigilancia - fix: esperar a que cada audio empiece (PLAYING) antes de dar por terminado, para no cortar sirena ni mensaje

This commit is contained in:
minguezsanzjuanjose
2026-08-02 14:40:55 +02:00
parent 3723f39371
commit e31478efc6

View File

@@ -92,13 +92,37 @@ def _url_audio(texto, nombre="anuncio.mp3"):
return f"http://{_ip_lan()}:{config.GOOGLE_TV_PUERTO}/{os.path.basename(ruta)}"
def _esperar_fin(cast, timeout=90):
def _esperar_reproduccion(cast, timeout=15):
"""Espera a que la reproduccion realmente empiece (PLAYING/BUFFERING)."""
mc = cast.media_controller
t0 = time.time()
while time.time() - t0 < timeout:
try:
st = mc.status
if st.player_state in ("PLAYING", "BUFFERING"):
return True
if st.player_state == "IDLE" and st.idle_reason == "FINISHED":
return True
except Exception:
pass
time.sleep(1)
return False
def _esperar_fin(cast, timeout=90):
"""Espera a que termine (IDLE) despues de haber empezado a reproducir."""
mc = cast.media_controller
t0 = time.time()
visto_playing = False
while time.time() - t0 < timeout:
try:
st = mc.status
if st.player_state == "PLAYING":
visto_playing = True
if st.player_state == "IDLE":
if st.idle_reason == "FINISHED":
return True
if visto_playing:
return True
except Exception:
pass
@@ -180,6 +204,7 @@ def _reproducir_y_esperar(c, url, content_type, titulo, desc):
c.media_controller.play_media(url, content_type, title=titulo)
c.media_controller.block_until_active(timeout=10)
print(f"[Voz] {desc}")
_esperar_reproduccion(c)
return _esperar_fin(c)