Skip to content
This repository has been archived by the owner on Dec 27, 2021. It is now read-only.

Commit

Permalink
Add clip() method (inspired by node-sonos-http-api)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmichalak committed Nov 1, 2017
1 parent 13c0d04 commit eeef5f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
27 changes: 24 additions & 3 deletions src/main/java/com/vmichalak/sonoscontroller/SonosDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/vmichalak/sonoscontroller/model/TrackInfo.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -38,7 +38,7 @@ public TrackMetadata getMetadata() {
@Override
public String toString() {
return "TrackInfo{" +
"playlistPosition=" + playlistPosition +
"queueIndex=" + queueIndex +
", duration='" + duration + '\'' +
", position='" + position + '\'' +
", uri='" + uri + '\'' +
Expand Down

0 comments on commit eeef5f8

Please sign in to comment.