public final class Spotify extends Object
SpotifyPlayer
object.
The class ensures that only one instance of SpotifyPlayer
is created.Modifier and Type | Method and Description |
---|---|
static boolean |
awaitDestroyPlayer(Object reference,
long timeout,
TimeUnit timeUnit)
Release a player resource.
|
static void |
destroyPlayer(Object reference)
Release a player resource.
|
static SpotifyPlayer |
getPlayer(Config config,
Object reference,
SpotifyPlayer.InitializationObserver observer)
Get (or possibly create) an instance of the player.
|
static SpotifyPlayer |
getPlayer(SpotifyPlayer.Builder builder,
Object reference,
SpotifyPlayer.InitializationObserver observer)
Get (or possibly create) an instance of the player with custom components.
|
static int |
getReferenceCount()
Check how many references to the player currently exists.
|
public static SpotifyPlayer getPlayer(Config config, Object reference, SpotifyPlayer.InitializationObserver observer)
If you have multiple fragments which all want their own player, you should take care to make sure that only one thread calls this to initialize the player.
SUPER IMPORTANT: you must call destroyPlayer(Object)
when you are finished
using the Player, otherwise native resources will be leaked.
config
- Configuration object for the player.reference
- Object which owns this player. This same object must be passed to
destroyPlayer(Object)
.observer
- Observer to be notified when initialization is complete. In the event that
the player has already been initialized, then
SpotifyPlayer.InitializationObserver.onInitialized(SpotifyPlayer)
will be called directly.SpotifyPlayer
instancegetPlayer(Config, Object, SpotifyPlayer.InitializationObserver)
public static SpotifyPlayer getPlayer(SpotifyPlayer.Builder builder, Object reference, SpotifyPlayer.InitializationObserver observer)
If you have multiple fragments which all want their own player, you should take care to make sure that only one thread calls this to initialize the player.
SUPER IMPORTANT: you must call destroyPlayer(Object)
when you are finished
using the Player, otherwise native resources will be leaked.
builder
- SpotifyPlayer.Builder
object.reference
- Object which owns this player. This same object must be passed to
destroyPlayer(Object)
.observer
- Observer to be notified when initialization is complete. In the event that
the player has already been initialized, then
SpotifyPlayer.InitializationObserver.onInitialized(SpotifyPlayer)
will be called directly.SpotifyPlayer
instanceSpotifyPlayer.Builder
public static void destroyPlayer(Object reference)
Since there is only a single instance of the player, it is
refcounted using Object instances, which must be correctly registered in the call to
getPlayer(Config, Object, SpotifyPlayer.InitializationObserver)
The player will only be actually destroyed once the last object has released it.
This convenience method calls SpotifyPlayer.shutdown()
under the hood allowing
all queued and unfinished commands to complete therefore it is wrong to assume
that the player is completely destroyed after calling it.
To make sure that the player is completely shut down use awaitDestroyPlayer(Object, long, java.util.concurrent.TimeUnit)
or poll for the state with: SpotifyPlayer.isTerminated()
Making sure that the player instance is destroyed is essential if you're planning on recreating it. Trying to create a new player when the old one is still shutting down will result in an error.
Ideally, if you are using a SpotifyPlayer
from an
Activity or Fragment, you should call this method in the onDestroy()
overridden method.
This method is safe to call even if your Activity/Fragment exited early and never finished
creating its player. Bottom line -- always call this.
reference
- Object which owned this player.public static boolean awaitDestroyPlayer(Object reference, long timeout, TimeUnit timeUnit) throws InterruptedException
A convenience method that will release a player resource and perform synchronous shutdown if there's no remaining references to the Player instance. Calling this method is an equivalent of:
player.shutdown();
player.awaitTermination(timeout, timeUnit);
Making sure that the player instance is destroyed is essential if you're planning on recreating it. Trying to create a new player when the old one is still shutting down will result in an error.
Ideally, if you are using a SpotifyPlayer
from an
Activity or Fragment, you should call this method in the onDestroy() overridden method.
This method is safe to call even if your Activity/Fragment exited early and never finished
creating its player.
reference
- Object which owns this playertimeout
- the maximum time to waittimeUnit
- the time unit of the timeout argumentInterruptedException
- if interrupted while waitingdestroyPlayer(Object)
public static int getReferenceCount()