lib/sonos/endpoint/a_v_transport.rb in sonos-0.3.0 vs lib/sonos/endpoint/a_v_transport.rb in sonos-0.3.1

- old
+ new

@@ -9,34 +9,42 @@ def now_playing response = send_transport_message('GetPositionInfo') body = response.body[:get_position_info_response] doc = Nokogiri::XML(body[:track_meta_data]) + # No music + return nil if doc.children.length == 0 + art_path = doc.xpath('//upnp:albumArtURI').inner_text # TODO: No idea why this is necessary. Maybe its a Nokogiri thing art_path.sub!('/getaa?s=1=x-sonos-http', '/getaa?s=1&u=x-sonos-http') { title: doc.xpath('//dc:title').inner_text, artist: doc.xpath('//dc:creator').inner_text, album: doc.xpath('//upnp:album').inner_text, + info: doc.xpath('//r:streamContent').inner_text, queue_position: body[:track], track_duration: body[:track_duration], current_position: body[:rel_time], uri: body[:track_uri], album_art: "http://#{self.ip}:#{Sonos::PORT}#{art_path}" } end + def has_music? + !now_playing.nil? + end + # Pause the currently playing track. def pause send_transport_message('Pause') end # Play the currently selected track or play a stream. - # @param [String] optional uri of the track to play. Leaving this blank, plays the current track. + # @param [String] uri Optional uri of the track to play. Leaving this blank, plays the current track. def play(uri = nil) # Play a song from the uri set_av_transport_uri(uri) and return if uri # Play the currently selected track @@ -76,10 +84,28 @@ # Save queue def save_queue(title) send_transport_message('SaveQueue', "<Title>#{title}</Title><ObjectID></ObjectID>") end + # Adds a track to the queue + # @param[String] uri Uri of track + # @return[Integer] Queue position of the added track + def add_to_queue(uri) + response = send_transport_message('AddURIToQueue', "<EnqueuedURI>#{uri}</EnqueuedURI><EnqueuedURIMetaData></EnqueuedURIMetaData><DesiredFirstTrackNumberEnqueued>0</DesiredFirstTrackNumberEnqueued><EnqueueAsNext>1</EnqueueAsNext>") + # TODO yeah, this error handling is a bit soft. For consistency's sake :) + pos = response.xpath('.//FirstTrackNumberEnqueued').text + if pos.length != 0 + pos.to_i + end + end + + # Removes a track from the queue + # @param[String] object_id Track's queue ID + def remove_from_queue(object_id) + send_transport_message('RemoveTrackFromQueue', "<ObjectID>#{object_id}</ObjectID><UpdateID>0</UpdateID></u:RemoveTrackFromQueue>") + end + # Join another speaker's group. # Trying to call this on a stereo pair slave will fail. def join(master) set_av_transport_uri('x-rincon:' + master.uid.sub('uuid:', '')) end @@ -94,10 +120,10 @@ # Trying to call this on a stereo pair slave will fail. def ungroup send_transport_message('BecomeCoordinatorOfStandaloneGroup') end -private + private # Play a stream. def set_av_transport_uri(uri) send_transport_message('SetAVTransportURI', "<CurrentURI>#{uri}</CurrentURI><CurrentURIMetaData></CurrentURIMetaData>") end