From 26283dab75540d2dfda9351c3e86ec1a9cd251f9 Mon Sep 17 00:00:00 2001 From: Gwendal Roulleau Date: Wed, 1 Nov 2023 08:50:01 +0100 Subject: [PATCH] [webaudio] Allow stopping play (#3766) Signed-off-by: Gwendal Roulleau Co-authored-by: Gwendal Roulleau --- .../internal/webaudio/WebAudioAudioSink.java | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/webaudio/WebAudioAudioSink.java b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/webaudio/WebAudioAudioSink.java index 36e492ee772..389a935a204 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/webaudio/WebAudioAudioSink.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/webaudio/WebAudioAudioSink.java @@ -24,7 +24,6 @@ import org.openhab.core.audio.AudioSinkAsync; import org.openhab.core.audio.AudioStream; import org.openhab.core.audio.StreamServed; -import org.openhab.core.audio.URLAudioStream; import org.openhab.core.audio.UnsupportedAudioFormatException; import org.openhab.core.audio.UnsupportedAudioStreamException; import org.openhab.core.events.EventPublisher; @@ -67,29 +66,19 @@ public void processAsynchronously(@Nullable AudioStream audioStream) if (audioStream == null) { // in case the audioStream is null, this should be interpreted as a request to end any currently playing // stream. - logger.debug("Web Audio sink does not support stopping the currently playing stream."); + sendEvent(""); return; } logger.debug("Received audio stream of format {}", audioStream.getFormat()); - if (audioStream instanceof URLAudioStream urlAudioStream) { - try (AudioStream stream = urlAudioStream) { - // in this case only, we need to close the stream by ourself in a try with block, - // because nothing will consume it - // it is an external URL, so we can directly pass this on. - sendEvent(urlAudioStream.getURL()); - } catch (IOException e) { - logger.debug("Error while closing the audio stream: {}", e.getMessage(), e); - } - } else { - // we need to serve it for a while and make it available to multiple clients - try { - StreamServed servedStream = audioHTTPServer.serve(audioStream, 10, true); - // we will let the HTTP servlet run the delayed task when finished with the stream - servedStream.playEnd().thenRun(() -> this.playbackFinished(audioStream)); - sendEvent(servedStream.url()); - } catch (IOException e) { - logger.warn("Cannot precache the audio stream to serve it", e); - } + + // we need to serve it for a while and make it available to multiple clients + try { + StreamServed servedStream = audioHTTPServer.serve(audioStream, 10, true); + // we will let the HTTP servlet run the delayed task when finished with the stream + servedStream.playEnd().thenRun(() -> this.playbackFinished(audioStream)); + sendEvent(servedStream.url()); + } catch (IOException e) { + logger.warn("Cannot precache the audio stream to serve it", e); } }