SKPlayer

public struct SKPlayer

A structure used to make requests to the Spotify Web API regarding the current authenticated user’s Spotify player.

  • Creates and returns the request used to get the current authenticated user’s recent tracks.

    Declaration

    Swift

    public static func makeRecentTracksRequest(startAfterDate: Date?, limit: Int?) -> SKRequest

    Parameters

    startAfterDate

    A date after which to request the tracks. The response will include any tracks within the limit that have been played after, but not including, the given date and time, with millisecond precision.

    limit

    The number of items to return.

    Return Value

    An SKRequest instance with which to perform the API request.

  • Creates and returns the request used to get the current authenticated user’s recent tracks.

    Declaration

    Swift

    public static func makeRecentTracksRequest(endBeforeDate: Date?, limit: Int?) -> SKRequest

    Parameters

    endBeforeDate

    A date before which to request the tracks. The response will include any tracks within the limit that have been played up to, but not including, the given date and time, with millisecond precision.

    limit

    The number of items to return.

    Return Value

    An SKRequest instance with which to perform the API request.

  • Gets the current authenticated user’s recently played tracks.

    Note

    This method uses the SPTAuth default instance session to authenticate the underlying request. If this session does not contain a valid access token, the request will result in an error. The access token must have been issued on behalf of the current user.

    Reading the current user’s recently played tracks also requires authorization of the user-read-recently-played scope. See Using Scopes for more details.

    Declaration

    Swift

    public static func getRecentTracks(after date: Date? = nil, limit: Int? = nil, completion handler: @escaping (CursorPage<SKRecentTrack>?, Error?) -> Void)

    Parameters

    date

    A date after which to request the tracks. The response will include any tracks within the limit that have been played after, but not including, the given date and time, with millisecond precision. The default value is nil, which returns the most recent tracks.

    limit

    The number of items to return. The default value is nil.

    handler

    The callback handler for the request. The parameters for this handler are:

    • tracks: A cursor-based paginated list of recent artists, if any.
    • error: An error identifying if and why the request failed, or nil if the request was successful.

  • Gets the current authenticated user’s recently played tracks.

    Note

    This method uses the SPTAuth default instance session to authenticate the underlying request. If this session does not contain a valid access token, the request will result in an error. The access token must have been issued on behalf of the current user.

    Reading the current user’s recently played tracks also requires authorization of the user-read-recently-played scope. See Using Scopes for more details.

    Declaration

    Swift

    public static func getRecentTracks(before date: Date?, limit: Int? = nil, completion handler: @escaping (CursorPage<SKRecentTrack>?, Error?) -> Void)

    Parameters

    date

    A date before which to request the tracks. The response will include any tracks within the limit that have been played up to, but not including, the given date and time, with millisecond precision.

    limit

    The number of items to return. The default value is nil.

    handler

    The callback handler for the request. The parameters for this handler are:

    • tracks: A cursor-based paginated list of recent artists, if any.
    • error: An error identifying if and why the request failed, or nil if the request was successful.

  • Creates and returns the request used to get the current authenticated user’s available devices.

    Declaration

    Swift

    public static func makeAvailableDevicesRequest() -> SKRequest

    Return Value

    An SKRequest instance with which to perform the API request.

  • Gets a list of the current authenticated user’s available devices.

    Note

    This method uses the SPTAuth default instance session to authenticate the underlying request. If this session does not contain a valid access token, the request will result in an error. The access token must have been issued on behalf of the current user.

    Reading the list of the current user’s available devices also requires authorization of the user-read-playback-state scope. See Using Scopes for more details.

    Declaration

    Swift

    public static func getAvailableDevices(completion handler: @escaping ([SKDevice]?, Error?) -> Void)

    Parameters

    handler

    The callback handler for the request. The parameters for this handler are:

    • devices: An array of available devices, if any.
    • error: An error identifying if and why the request failed, or nil if the request was successful.

  • Creates and returns the request used to get the current authenticated user’s available devices.

    Declaration

    Swift

    public static func makePlaybackStateRequest(locale: Locale?) -> SKRequest

    Parameters

    locale

    The locale-specific storefront/market from which to request.

    Return Value

    An SKRequest instance with which to perform the API request.

  • Gets the active Spotify player’s current playback state, including the currently playing track if applicable.

    Note

    This method uses the SPTAuth default instance session to authenticate the underlying request. If this session does not contain a valid access token, the request will result in an error. The access token must have been issued on behalf of the current user.

    Reading the current Spotify player’s playback information also requires authorization of the user-read-playback-state scope. See Using Scopes for more details.

    Declaration

    Swift

    public static func getPlaybackState(for locale: Locale? = SKCatalog.local.locale, completion handler: @escaping (SKPlaybackState?, Error?) -> Void)

    Parameters

    locale

    The locale-specific storefront/market from which to request. The default value is the locale for the shared SKCatalog instance, which by default represents the user’s region settings at the time the method is called.

    handler

    The callback handler for the request. The parameters for this handler are:

    • state: An SKPlaybackState instance.
    • error: An error identifying if and why the request failed, or nil if the request was successful.

  • Creates and returns the request used to transfer playback to another device.

    Declaration

    Swift

    public static func makeTransferRequest(deviceID: String, forcePlayback: Bool) -> SKRequest

    Parameters

    deviceID

    The ID of the device on which playback should be transferred.

    forcePlayback

    A Boolean value indicating whether the new device should begin playback: true ensures playback happens on the new device; false keeps the current playback state. Note that providing false will not pause playback.

    Return Value

    An SKRequest instance with which to perform the API request.

  • Transfers playback to the given device.

    Important

    The given SKDevice instance must have an ID. If the device contains a nil ID value, then this method will immediately call the handler and provide an SKError instance denoting a bad request.

    Note

    This method uses the SPTAuth default instance session to authenticate the underlying request. If this session does not contain a valid access token, the request will result in an error. The access token must have been issued on behalf of the current user.

    Modifying the playback state, to include the active device, also requires authorization of the user-modify-playback-state scope. See Using Scopes for more details.

    Declaration

    Swift

    public static func transfer(to device: SKDevice, forcingPlayback forcePlayback: Bool = false, completion handler: @escaping SKErrorHandler)

    Parameters

    device

    The device on which playback should be transferred.

    forcePlayback

    A Boolean value indicating whether the new device should begin playback: true ensures playback happens on the new device; false keeps the current playback state. The default value is false. Note that providing false will not pause playback.

    handler

    The callback handler for the request, providing an error identifying if and why the request failed, or nil if the request was successful.