diff --git a/src/main/java/com/vmichalak/sonoscontroller/SonosDevice.java b/src/main/java/com/vmichalak/sonoscontroller/SonosDevice.java index ebb2aff..db69cfe 100755 --- a/src/main/java/com/vmichalak/sonoscontroller/SonosDevice.java +++ b/src/main/java/com/vmichalak/sonoscontroller/SonosDevice.java @@ -34,9 +34,9 @@ public void play() throws IOException, SonosControllerException { * @throws IOException * @throws SonosControllerException */ - public void playUri(String uri, String meta) throws IOException, SonosControllerException { + public void playUri(String uri, String metadata) throws IOException, SonosControllerException { CommandBuilder.transport("SetAVTransportURI").put("InstanceID", "0").put("CurrentURI", uri) - .put("CurrentURIMetaData", meta).executeOn(this.ip); + .put("CurrentURIMetaData", metadata).executeOn(this.ip); this.play(); } @@ -48,7 +48,28 @@ public void playFromQueue(int queueIndex) throws IOException, SonosControllerExc if(queueIndex < 0) { throw new IllegalArgumentException("Queue index cannot be < 0."); } this.playUri("x-rincon-queue:" + this.getSpeakerInfo().getLocalUID() + "#0", ""); CommandBuilder.transport("Seek").put("InstanceID", "0").put("Unit", "TRACK_NR") - .put("Target", String.valueOf(queueIndex + 1)).executeOn(this.ip); + .put("Target", String.valueOf(queueIndex)).executeOn(this.ip); + this.play(); + } + + /** + * Pause current music, Play URI and resume (very useful for announcement). + * clip is a blocking method. Take care ! + * @param uri URI of a stream to be played. + * @param metadata The track metadata to show in the player (DIDL format). + * @throws IOException + * @throws SonosControllerException + * @throws InterruptedException + */ + public void clip(String uri, String metadata) throws IOException, SonosControllerException, InterruptedException { + TrackInfo previous = this.getCurrentTrackInfo(); + this.playUri(uri, metadata); + while (this.getPlayState() != PlayState.STOPPED) { + Thread.sleep(500); + } + this.playUri("x-rincon-queue:" + this.getSpeakerInfo().getLocalUID() + "#0", ""); + this.playFromQueue(previous.getQueueIndex()); + this.seek(previous.getPosition()); this.play(); } diff --git a/src/main/java/com/vmichalak/sonoscontroller/model/TrackInfo.java b/src/main/java/com/vmichalak/sonoscontroller/model/TrackInfo.java index 8d1009a..34ef8ef 100644 --- a/src/main/java/com/vmichalak/sonoscontroller/model/TrackInfo.java +++ b/src/main/java/com/vmichalak/sonoscontroller/model/TrackInfo.java @@ -1,22 +1,22 @@ package com.vmichalak.sonoscontroller.model; public class TrackInfo { - private final int playlistPosition; + private final int queueIndex; private final String duration; private final String position; private final String uri; private final TrackMetadata metadata; - public TrackInfo(int playlistPosition, String duration, String position, String uri, TrackMetadata metadata) { - this.playlistPosition = playlistPosition; + public TrackInfo(int queueIndex, String duration, String position, String uri, TrackMetadata metadata) { + this.queueIndex = queueIndex; this.duration = duration; this.position = position; this.uri = uri; this.metadata = metadata; } - public int getPlaylistPosition() { - return playlistPosition; + public int getQueueIndex() { + return queueIndex; } public String getDuration() { @@ -38,7 +38,7 @@ public TrackMetadata getMetadata() { @Override public String toString() { return "TrackInfo{" + - "playlistPosition=" + playlistPosition + + "queueIndex=" + queueIndex + ", duration='" + duration + '\'' + ", position='" + position + '\'' + ", uri='" + uri + '\'' +