public final class SpotifyPlayer extends com.spotify.jni.NativeObject implements Player
Normally you should not need to directly instantiate this class, instead you should use
Spotify.getPlayer(Config, Object, SpotifyPlayer.InitializationObserver)
to obtain a refcounted instance for you.
AudioController
that uses AudioTrack
to play music.
It is possible to override this behavior by implementing a custom AudioController
and passing it to SpotifyPlayer.Builder
:
Config config = new Config(...);
MyAudioController myAudioController = new MyAudioController();
Player.Builder builder = new Player.Builder(config);
builder.setAudioController(myAudioController);
Spotify = new Spotify();
spotify.getPlayer(builder, this);
Handler
to SpotifyPlayer.Builder
:
// Assuming this code runs in a different thread
Config config = new Config(...);
Handler myHandler = new Handler();
Player.Builder builder = new Player.Builder(config);
builder.setCallbackHandler(myHandler);
Spotify = new Spotify();
spotify.getPlayer(builder, this);
Modifier and Type | Class and Description |
---|---|
static class |
SpotifyPlayer.Builder
Builder that can be used to create a customized instance of the
SpotifyPlayer . |
static interface |
SpotifyPlayer.InitializationObserver
Observer interface used to know when the player is fully initialized.
|
Player.AudioDeliveredCallback, Player.AudioFlushCallback, Player.NotificationCallback, Player.OperationCallback
Modifier and Type | Method and Description |
---|---|
boolean |
addConnectionStateCallback(ConnectionStateCallback callback) |
boolean |
addNotificationCallback(Player.NotificationCallback callback) |
static SpotifyPlayer |
create(Config config)
Create a new player instance.
|
static SpotifyPlayer |
create(Config config,
SpotifyPlayer.InitializationObserver observer)
Create a new player instance with initialization observer.
|
void |
destroy()
Destroys the native player.
|
Metadata |
getMetadata() |
PlaybackState |
getPlaybackState() |
void |
initialize(Config config)
Initializes the native player.
|
boolean |
isInitialized() |
boolean |
isLoggedIn() |
boolean |
isShutdown() |
boolean |
isTerminated() |
boolean |
login(String oauthToken)
Login to the player using OAuth token.
|
boolean |
logout()
Logout from the player.
|
int |
onAudioDelivered(short[] samples,
int sampleCount,
int sampleRate,
int channels) |
void |
onAudioFlush() |
void |
pause(Player.OperationCallback callback)
Pause playback.
|
void |
playUri(Player.OperationCallback callback,
String uri,
int index,
int positionInMs)
Play a URI with the player.
|
void |
queue(Player.OperationCallback callback,
String uri)
Add a URI to be played next in the queue.
|
void |
refreshCache()
Triggers cache refresh and possible eviction of state data.
|
boolean |
removeConnectionStateCallback(ConnectionStateCallback callback) |
boolean |
removeNotificationCallback(Player.NotificationCallback callback) |
void |
resume(Player.OperationCallback callback)
Resume playback if paused.
|
void |
seekToPosition(Player.OperationCallback callback,
int positionInMs)
Jump to a given position in the current track.
|
void |
setConnectivityStatus(Player.OperationCallback callback,
Connectivity status)
The native SDK needs to be told what the current connection state is in order to determine
ideal connection settings to Spotify's servers.
|
void |
setPlaybackBitrate(Player.OperationCallback callback,
PlaybackBitrate playbackBitrate)
Set the bitrate of the player to specified value.
|
void |
setRepeat(Player.OperationCallback callback,
boolean enabled)
Enable or disable repeat.
|
void |
setShuffle(Player.OperationCallback callback,
boolean enabled)
Enable or disable shuffling.
|
void |
shutdown() |
void |
skipToNext(Player.OperationCallback callback)
Skip to the next track in the play queue.
|
void |
skipToPrevious(Player.OperationCallback callback)
Skip to the previous track in the play queue.
|
public static SpotifyPlayer create(Config config)
config
- Configuration variables used during native initializationpublic static SpotifyPlayer create(Config config, SpotifyPlayer.InitializationObserver observer)
config
- Configuration variables used during native initializationobserver
- Observer to be notified when initialization is complete (or fails)public boolean isInitialized()
public boolean isLoggedIn()
public boolean isTerminated()
public boolean isShutdown()
public void shutdown()
public boolean login(String oauthToken)
ConnectionStateCallback.onLoggedIn()
or the
ConnectionStateCallback.onLoginFailed(Error)
callback.public boolean logout()
ConnectionStateCallback.onLoggedOut()
callback.public boolean addConnectionStateCallback(ConnectionStateCallback callback)
addConnectionStateCallback
in interface Player
public boolean removeConnectionStateCallback(ConnectionStateCallback callback)
removeConnectionStateCallback
in interface Player
public void playUri(Player.OperationCallback callback, String uri, int index, int positionInMs)
public void queue(Player.OperationCallback callback, String uri)
playUri(com.spotify.sdk.android.player.Player.OperationCallback,String,int,int)
for a list of supported URIs.public void pause(Player.OperationCallback callback)
public void resume(Player.OperationCallback callback)
public void skipToNext(Player.OperationCallback callback)
skipToNext
in interface Player
callback
- operation result callbackpublic void skipToPrevious(Player.OperationCallback callback)
seekToPosition(com.spotify.sdk.android.player.Player.OperationCallback,int)
with 0.skipToPrevious
in interface Player
callback
- operation result callbackpublic void seekToPosition(Player.OperationCallback callback, int positionInMs)
seekToPosition
in interface Player
positionInMs
- Position in millisecondscallback
- operation result callbackpublic void setShuffle(Player.OperationCallback callback, boolean enabled)
setShuffle
in interface Player
callback
- operation result callbackenabled
- True to enable shuffling, false to disable it.public void setRepeat(Player.OperationCallback callback, boolean enabled)
public void setPlaybackBitrate(Player.OperationCallback callback, PlaybackBitrate playbackBitrate)
setPlaybackBitrate
in interface Player
callback
- operation result callbackplaybackBitrate
- The bitrate to set playback to. By default is is set to
PlaybackBitrate.BITRATE_NORMAL
for details
public void setConnectivityStatus(Player.OperationCallback callback, Connectivity status)
Connectivity
class and the demo
app source code which is shipped with the Spotify SDK for Android.setConnectivityStatus
in interface Player
callback
- operation result callbackstatus
- Connectivity statusConnectivity.fromNetworkType(int)
public boolean addNotificationCallback(Player.NotificationCallback callback)
addNotificationCallback
in interface Player
public boolean removeNotificationCallback(Player.NotificationCallback callback)
removeNotificationCallback
in interface Player
public void initialize(Config config)
Player
initialize
in interface Player
config
- The configuration object to init native player with.public void destroy()
Player
public int onAudioDelivered(short[] samples, int sampleCount, int sampleRate, int channels)
public void onAudioFlush()
public void refreshCache()
Player
refreshCache
in interface Player
public Metadata getMetadata()
getMetadata
in interface Player
public PlaybackState getPlaybackState()
getPlaybackState
in interface Player