Class: RSpotify::Player

Inherits:
Base
  • Object
show all
Defined in:
lib/rspotify/player.rb

Instance Attribute Summary

Attributes inherited from Base

#external_urls, #href, #id, #type, #uri

Instance Method Summary collapse

Methods inherited from Base

#complete!, #embed, find, #method_missing, #respond_to?, search

Constructor Details

#initialize(user, options = {}) ⇒ Player

Returns a new instance of Player.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rspotify/player.rb', line 4

def initialize(user, options = {})
  @user = user

  @repeat_state           = options['repeat_state']
  @shuffle_state          = options['shuffle_state']
  @progress               = options['progress_ms']
  @is_playing             = options['is_playing']
  @currently_playing_type = options['currently_playing_type']

  @track = if options['track']
    Track.new options['track']
  end

  @device = if options['device']
    Device.new options['device']
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RSpotify::Base

Instance Method Details

#currently_playingObject



154
155
156
157
158
159
# File 'lib/rspotify/player.rb', line 154

def currently_playing
  url = "me/player/currently-playing"
  response = RSpotify.resolve_auth_request(@user.id, url)
  return response if RSpotify.raw_response
  Track.new response["item"]
end

#nextObject

Skip User’s Playback To Next Track

Examples:

player = user.player
player.next


129
130
131
132
# File 'lib/rspotify/player.rb', line 129

def next
  url = 'me/player/next'
  User.oauth_post(@user.id, url, {})
end

#pauseObject

Pause the user’s currently active player

Examples:

player = user.player
player.pause


99
100
101
102
# File 'lib/rspotify/player.rb', line 99

def pause
  url = 'me/player/pause'
  User.oauth_put(@user.id, url, {})
end

#play(device_id = nil, params = {}) ⇒ Object

Play the user’s currently active player or specific device If ‘device_id` is not passed, the currently active spotify app will be triggered

Examples:

player = user.player
player.play


67
68
69
70
71
72
# File 'lib/rspotify/player.rb', line 67

def play(device_id = nil, params = {})
  url = "me/player/play"
  url = device_id.nil? ? url : "#{url}?device_id=#{device_id}"

  User.oauth_put(@user.id, url, params.to_json)
end

#play_context(device_id = nil, uri) ⇒ Object

Allow user to play a specific context(albums, artists & playlists). If ‘device_id` is not passed, the currently active spotify app will be triggered

Examples:

player = user.player
player.play_context(nil,"spotify:album:1Je1IMUlBXcx1Fz0WE7oPT")


32
33
34
35
# File 'lib/rspotify/player.rb', line 32

def play_context(device_id = nil, uri)
  params = {"context_uri": uri}
  play(device_id, params)
end

#play_track(device_id = nil, uri) ⇒ Object

Examples:

player = user.player
player.play_track(nil, "spotify:track:4iV5W9uYEdYUVa79Axb7Rh")
# User must be a premium subscriber for this feature to work.


56
57
58
59
# File 'lib/rspotify/player.rb', line 56

def play_track(device_id = nil, uri)
  params = {"uris": [uri]}
  play(device_id, params)
end

#play_tracks(device_id = nil, uris) ⇒ Object

Allow user to play a list of tracks. If ‘device_id` is not passed, the currently active spotify app will be triggered

Examples:

player = user.player
tracks_uris = ["spotify:track:4iV5W9uYEdYUVa79Axb7Rh", "spotify:track:1301WleyT98MSxVHPZCA6M"]
player.play_tracks(nil, tracks_uris)


44
45
46
47
# File 'lib/rspotify/player.rb', line 44

def play_tracks(device_id = nil, uris)
  params = {"uris": uris}
  play(device_id, params)
end

#playing?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rspotify/player.rb', line 22

def playing?
  is_playing
end

#previousObject

Skip User’s Playback To Previous Track

Examples:

player = user.player
player.previous


139
140
141
142
# File 'lib/rspotify/player.rb', line 139

def previous
  url = 'me/player/previous'
  User.oauth_post(@user.id, url, {})
end

#repeat(device_id: nil, state: "context") ⇒ Object

Toggle the current user’s player repeat status. If ‘device_id` is not passed, the currently active spotify app will be triggered. If `state` is not passed, the currently active context will be set to repeat.

Examples:

player = user.player
player.repeat(state: 'track')

Parameters:

  • device_id (String) (defaults to: nil)

    the ID of the device to set the repeat state on.

  • state (String) (defaults to: "context")

    the repeat state. Defaults to the current play context.

See Also:



86
87
88
89
90
91
92
# File 'lib/rspotify/player.rb', line 86

def repeat(device_id: nil, state: "context")
  url = "me/player/repeat"
  url += "?state=#{state}"
  url += "&device_id=#{device_id}" if device_id

  User.oauth_put(@user.id, url, {})
end

#seek(position_ms) ⇒ Object



161
162
163
164
# File 'lib/rspotify/player.rb', line 161

def seek(position_ms)
  url = "me/player/seek?position_ms=#{position_ms}"
  User.oauth_put(@user.id, url, {})
end

#shuffle(device_id: nil, state: true) ⇒ Object

Toggle the current user’s shuffle status. If ‘device_id` is not passed, the currently active spotify app will be triggered. If `state` is not passed, shuffle mode will be turned on.

Examples:

player = user.player
player.shuffle(state: false)

Parameters:

  • device_id (String) (defaults to: nil)

    the ID of the device to set the shuffle state on.

  • state (String) (defaults to: true)

    the shuffle state. Defaults to turning the shuffle behavior on.

See Also:



116
117
118
119
120
121
122
# File 'lib/rspotify/player.rb', line 116

def shuffle(device_id: nil, state: true)
  url = "me/player/shuffle"
  url += "?state=#{state}"
  url += "&device_id=#{device_id}" if device_id

  User.oauth_put(@user.id, url, {})
end

#volume(percent) ⇒ Object

Update the user’s currently active player volume

Examples:

player = user.player
player.volume(50)


149
150
151
152
# File 'lib/rspotify/player.rb', line 149

def volume(percent)
  url = "me/player/volume?volume_percent=#{percent}"
  User.oauth_put(@user.id, url, {})
end