Class: Spotify::SDK::Connect::PlaybackState

Inherits:
Model
  • Object
show all
Defined in:
lib/spotify/sdk/connect/playback_state.rb

Instance Attribute Summary

Attributes inherited from Model

#parent

Instance Method Summary collapse

Methods inherited from Model

alias_attribute, hash_selector, #initialize, #to_h, #to_json

Constructor Details

This class inherits a constructor from Spotify::SDK::Model

Instance Method Details

#artistSpotify::SDK::Artist

Get the main artist for the currently playing track.

Examples:

@sdk.connect.playback.artist

Returns:



123
124
125
# File 'lib/spotify/sdk/connect/playback_state.rb', line 123

def artist
  artists.first
end

#artistsArray

Get the artists for the currently playing track.

Examples:

@sdk.connect.playback.artists

Returns:

  • (Array)

    artists An array of artists wrapped in Spotify::SDK::Artist



109
110
111
112
113
# File 'lib/spotify/sdk/connect/playback_state.rb', line 109

def artists
  item[:artists].map do |artist|
    Spotify::SDK::Artist.new(artist, parent)
  end
end

#deviceSpotify::SDK::Connect::Device

Get the device the current playback is on.

Examples:

device = @sdk.connect.devices[0]
device.playback.device

Returns:



16
17
18
# File 'lib/spotify/sdk/connect/playback_state.rb', line 16

def device
  Spotify::SDK::Connect::Device.new(super, parent)
end

#itemSpotify::SDK::Item

Get the item for the currently playing track.

Examples:

@sdk.connect.playback.item

Returns:

  • (Spotify::SDK::Item)

    item The currently playing track, wrapped in Spotify::SDK::Item



135
136
137
138
139
# File 'lib/spotify/sdk/connect/playback_state.rb', line 135

def item
  raise "Playback information is not available if user has a private session enabled" if device.private_session?

  Spotify::SDK::Item.new(to_h, parent)
end

#playing?FalseClass, TrueClass

Is the current user playing a track?

Examples:

playback = @sdk.connect.playback
playback.playing?

Returns:

  • (FalseClass, TrueClass)

    is_playing True if user is currently performing playback.



29
# File 'lib/spotify/sdk/connect/playback_state.rb', line 29

alias_attribute :playing?, :is_playing

#positionInteger

What is the current position of the track?

Examples:

playback = @sdk.connect.playback
playback.position

Returns:

  • (Integer)

    position_ms In milliseconds, the position of the track.



82
# File 'lib/spotify/sdk/connect/playback_state.rb', line 82

alias_attribute :position, :progress_ms

#position_percentage(decimal_points = 2) ⇒ Float

How much percentage of the track is the position currently in?

Examples:

playback = @sdk.connect.playback
playback.position_percentage # => 7.30
playback.position_percentage(4) # => 7.3039

Parameters:

  • decimal_points (Integer) (defaults to: 2)

    How many decimal points to return

Returns:

  • (Float)

    percentage Completion percentage. Rounded to 2 decimal places.



95
96
97
98
99
# File 'lib/spotify/sdk/connect/playback_state.rb', line 95

def position_percentage(decimal_points=2)
  return nil if position.nil?

  ((position.to_f / item.duration.to_f) * 100).ceil(decimal_points)
end

#repeat_modeSymbol

What repeat mode is the current playback set to?

Options:

:off => This means no repeat is set.
:context => This means it will repeat within the same context.
:track => This will repeat the same track.

Examples:

playback = @sdk.connect.playback
playback.repeat # :off, :context, or :track

Returns:

  • (Symbol)

    repeat_mode Either :off, :context, or :track



56
57
58
# File 'lib/spotify/sdk/connect/playback_state.rb', line 56

def repeat_mode
  repeat_state.to_sym
end

#shuffling?FalseClass, TrueClass

Is the current playback set to shuffle?

Examples:

playback = @sdk.connect.playback
playback.shuffling?

Returns:

  • (FalseClass, TrueClass)

    is_shuffling True if shuffle is set.



40
# File 'lib/spotify/sdk/connect/playback_state.rb', line 40

alias_attribute :shuffling?, :shuffle_state

#timeTime

The current timestamp of the playback state

Examples:

playback = @sdk.connect.playback
playback.time

Returns:

  • (Time)

    time The accuracy time of the playback state.



69
70
71
# File 'lib/spotify/sdk/connect/playback_state.rb', line 69

def time
  Time.at(timestamp / 1000)
end