Skip to content

Commit

Permalink
🔀 Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoLegends committed Aug 26, 2017
2 parents dd27eb0 + 602150e commit a95f419
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ Note: Make sure your installation path doesn't contain any spaces.

The plugin has an extremely simple API that is focused just on playback. It consists of six functions clobbered onto `cordova.plugins.spotify`. In the following, treat all paths relative to that. The plugin handles all internal state and SDK initialization aspects automatically and hides these aspects from the developer.

All functions are asynchronous and return promises. The plugin automatically polyfills promise support through `es6-promise-plugin`.
All functions are asynchronous and return promises (due to the way the Cordova <-> native code bridge works). The plugin automatically polyfills promise support through `es6-promise-plugin`.

If the parameters have invalid values, an appropriate `Error` will be thrown immediately instead of returning a rejected promise. This is because invalid arguments are bugs and not runtime errors.
If any of the function parameters have invalid values, an appropriate `Error` will be thrown immediately instead of returning a rejected promise. This is because invalid arguments are bugs and not runtime errors.

### `getEventEmitter()`
### `getEventEmitter(): Promise<EventEmitter>`

Obtains an event emitter that relays the events fired by the native SDKs. The emitter will be created once and then returned on subsequent invocations.

The emitter implementation comes from [eventemitter3](https://github.com/primus/eventemitter3). See their Github page for more information.

The events emitted are the following:
- `connectionmessage`
- `loggedin`
Expand All @@ -51,11 +53,11 @@ The events emitted are the following:

In the case of `loginfailed`, `playbackevent` and `playbackerror`, the event contains a payload that describes what happened exactly. The payload is simply the name of the discriminant of the enum in the native SDK without the prefix (usually `kSp` or `kSpError`). See the offical documentation [here](https://spotify.github.io/android-sdk/player/com/spotify/sdk/android/player/Error.html) and [here](https://spotify.github.io/android-sdk/player/com/spotify/sdk/android/player/PlayerEvent.html) for all variants.

### `getPosition()`
### `getPosition(): Promise<number>`

Obtains the players position in _milliseconds_. If no track is currently loaded, returns 0.

### `play(trackUri: string, authOptions: object[, position: number])`
### `play(trackUri: string, authOptions: object[, position: number]): Promise`

Plays the track with the given Spotify URI.

Expand All @@ -69,18 +71,18 @@ Plays the track with the given Spotify URI.

`token` and `clientId` may change freely during runtime. The plugin will handle the required login / logout processes automatically when a new track is played.

### `pause()`
### `pause(): Promise`

Pauses playback. If no track is loaded, returns normally.

### `resume()`
### `resume(): Promise`

Resumes playback. If no track is loaded, the returned promise will be rejected with an error of type `not_playing`.

### `seekTo(position: number)`
### `seekTo(position: number): Promise`

Sets the playback position to the given value. If no track is loaded, the returned promise will be rejected with an error of type `not_playing`.

#### Parameters

- `position`: The position (in _milliseconds_) to seek to. Must be > 0.
- `position`: The position (in _milliseconds_) to seek to. Must be >= 0.
32 changes: 13 additions & 19 deletions src/android/ConnectionEventsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class ConnectionEventsHandler extends Emitter
implements ConnectionStateCallback {
private static final String TAG = "ConnectionEventsHandler";

private final ArrayList<Player.OperationCallback> loginCallbacks = new ArrayList<Player.OperationCallback>();
private final ArrayList<Runnable> logoutCallbacks = new ArrayList<Runnable>();
private Player.OperationCallback loginCallback = null;
private Runnable logoutCallback = null;

@Override
public void onConnectionMessage(String message) {
Expand All @@ -24,44 +24,38 @@ public void onConnectionMessage(String message) {

@Override
public void onLoggedIn() {
for (Player.OperationCallback item : this.loginCallbacks) {
if (item != null) {
item.onSuccess();
}
if (this.loginCallback != null) {
this.loginCallback.onSuccess();
this.loginCallback = null;
}
loginCallbacks.clear();

this.emit("loggedin");
}

public void onLoggedIn(Player.OperationCallback runnable) {
this.loginCallbacks.add(runnable);
this.loginCallback = runnable;
}

@Override
public void onLoggedOut() {
for (Runnable item : this.logoutCallbacks) {
if (item != null) {
item.run();
}
if (this.logoutCallback != null) {
this.logoutCallback.run();
this.logoutCallback = null;
}
logoutCallbacks.clear();

this.emit("loggedout");
}

public void onLoggedOut(Runnable runnable) {
this.logoutCallbacks.add(runnable);
this.logoutCallback = runnable;
}

@Override
public void onLoginFailed(Error error) {
for (Player.OperationCallback item : this.loginCallbacks) {
if (item != null) {
item.onError(error);
}
if (this.loginCallback != null) {
this.loginCallback.onError(error);
this.loginCallback = null;
}
loginCallbacks.clear();

this.emit("loginfailed", error);
}
Expand Down
2 changes: 2 additions & 0 deletions src/ios/CordovaSpotify.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ - (void) initAndPlay:(NSString*)clientId
if (!success) {
self.currentClientId = nil;
self.player.delegate = nil;
self.player.diskCache = nil;
self.player.playbackDelegate = nil;
self.player = nil;

Expand All @@ -73,6 +74,7 @@ - (void) initAndPlay:(NSString*)clientId
self.currentClientId = clientId;
self.player = [SPTAudioStreamingController sharedInstance];
self.player.delegate = self.audioStreamingDelegate;
self.player.diskCache = self.cache;
self.player.playbackDelegate = self.audioStreamingPlaybackDelegate;

[self loginAndPlay: accessToken
Expand Down
10 changes: 9 additions & 1 deletion www/cordova-spotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,20 @@ export function getEventEmitter() {
const reject = e => setTimeout(() => rej(e));

cordova.exec(event => {
// First callback invocation confirms the emitter's registration
// with the native code. The subsequent ones are actual events.
if (!emitterRegistered) {
emitterRegistered = true;
resolve(this);
} else {
setTimeout(() => emitter.emit(event.name, ...(event.args || [])));
}
}, err => reject(err), 'SpotifyConnector', 'registerEventsListener', []);
}, err => {
// Make sure we can try again
if (!emitterRegistered) {
emitter = null;
}
reject(err);
}, 'SpotifyConnector', 'registerEventsListener', []);
});
}

0 comments on commit a95f419

Please sign in to comment.