3 import interfaces
.Constants
;
4 import interfaces
.Track
.TrackFactory
;
6 import java
.io
.ByteArrayInputStream
;
7 import java
.io
.IOException
;
8 import java
.net
.UnknownHostException
;
10 import javax
.sound
.sampled
.UnsupportedAudioFileException
;
13 import api
.nodes
.AlbumNode
.AlbumFactory
;
14 import api
.nodes
.PlaylistNode
.PlaylistFactory
;
16 import reader
.DACPResponseParser
;
18 import util
.command
.Song
;
19 import util
.response
.ctrlint
.PlayStatusUpdate
;
21 public class StereoAmp
extends Thread
{
23 private final String host
;
24 private final int port
;
25 private final DACPResponseParser parser
;
27 private AudioPlayer player
;
30 public StereoAmp(String host
, int port
) {
35 parser
= new AmpResponseParser();
44 PlayStatusUpdate response
;
46 response
= (PlayStatusUpdate
)request("/ctrl-int/1/playstatusupdate?revision-number="+revision
);
49 response
= (PlayStatusUpdate
)request("/ctrl-int/1/playstatusupdate");
51 revision
= response
.revision
;
53 if (response
.state
== PlayStatusUpdate
.Status
.PLAYING
) {
55 current
= response
.active().currentTrackId
;
56 Song song
= (Song
)request("/ctrl-int/1/current");
58 if (song
!= null && song
.song() != null) {
60 String name
= response
.active().trackTitle
+ " by " + response
.active().trackArtist
;
61 player
= new AudioPlayer(name
, new ByteArrayInputStream(song
.song()));
62 System
.out
.println("playing");
63 player
.start(); //blocks until stream finishes playing
64 System
.out
.println("done");
66 if (!player
.stopped()) {
67 System
.out
.println("finished: next song");
68 request("/ctrl-int/1/nextitem?revision-number="+revision
);
74 catch (IOException ex
) {
76 } catch (UnsupportedAudioFileException e
) {
87 System
.out
.println("error: next song");
88 request("/ctrl-int/1/nextitem?revision-number="+revision
);
94 public void listen() {
101 rev
= "?revision-number="+revision
;
103 Response response
= request("/ctrl-int/1/playstatusupdate"+rev
);
105 if (response
== null) break;
107 if (response
.type() == Constants
.dmcp_status
) {
108 PlayStatusUpdate psu
= (PlayStatusUpdate
)response
;
110 revision
= psu
.revision
;
114 if (player
!= null && !player
.isPlaying()) player
.stop();
117 if (player
!= null && player
.isPlaying()) player
.pause();
120 if (player
!= null && !player
.isPlaying()) player
.play();
124 if (psu
.active()!=null) {
125 PlayStatusUpdate
.Active psua
= psu
.active();
126 System
.out
.printf("Playing %s by %s\n", psua
.trackTitle
, psua
.trackArtist
);
127 if (current
!= psua
.currentTrackId
) {
128 if (player
!= null) player
.stop();
132 System
.out
.println("Stopped");
136 System
.err
.println("Unexpected reponse type: " + response
.type().longName
);
142 private Response
request(String request
) {
144 return parser
.request(host
, port
, request
);
145 } catch (UnknownHostException e
) {
146 System
.err
.println("Unable to connect to " + host
);
148 } catch (IOException e
) {
149 System
.err
.println("Problem contacting " + host
);
156 private class AmpResponseParser
extends DACPResponseParser
{
159 public AlbumFactory
albumFactory() {
164 public PlaylistFactory
playlistFactory() {
169 public TrackFactory
trackFactory() {