Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
|
||
import { NativeModules } from 'react-native'; | ||
import NativeModuleEvents from 'react-native-events'; | ||
import Queue from './src/Queue'; | ||
import Spotify from './src/Spotify'; | ||
|
||
const Spotify = NativeModules.Spotify; | ||
|
||
NativeModuleEvents.registerNativeModule(Spotify); | ||
Spotify.queue = Queue; | ||
|
||
export default Spotify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
|
||
import EventEmitter from 'events'; | ||
import Spotify from './Spotify'; | ||
import SpotifyURI from 'spotify-uri'; | ||
|
||
|
||
const Queue = new EventEmitter(); | ||
|
||
let currentURI = null; | ||
let uris = []; | ||
let connected = null; | ||
|
||
|
||
|
||
// handle connection | ||
|
||
Spotify.on('login', () => { | ||
if(connected === null) | ||
{ | ||
connected = true; | ||
} | ||
}); | ||
|
||
Spotify.on('logout', () => { | ||
connected = null; | ||
Queue.clear(); | ||
}); | ||
|
||
Spotify.on('disconnect', () => { | ||
connected = false; | ||
}); | ||
|
||
Spotify.on('reconnect', () => { | ||
connected = true; | ||
}); | ||
|
||
|
||
|
||
// handle track queueing | ||
|
||
let tryQueuePlayback = null; | ||
tryQueuePlayback = () => { | ||
if(currentURI == null) | ||
{ | ||
return; | ||
} | ||
Spotify.playURI(currentURI, 0, 0, (error) => { | ||
if(error) | ||
{ | ||
// ensure we're logged in and we have uris | ||
if(connected !== null && uris.length > 0) | ||
{ | ||
if(!connected) | ||
{ | ||
// we must have failed because we weren't connected, so wait until we are | ||
Spotify.once('reconnect', tryQueuePlayback); | ||
} | ||
else | ||
{ | ||
// unknown error | ||
Queue.emit('queueError', error); | ||
} | ||
} | ||
return; | ||
} | ||
}); | ||
}; | ||
|
||
Spotify.on('trackDelivered', (event) => { | ||
if(uris.length > 0) | ||
{ | ||
// play the next song in the queue | ||
Queue.nextTrack(); | ||
} | ||
else if(currentURI != null) | ||
{ | ||
// the queue has finished | ||
currentURI = null; | ||
Queue.emit('queueFinish'); | ||
} | ||
}); | ||
|
||
|
||
|
||
// functions | ||
|
||
Queue.nextTrack = function() | ||
{ | ||
var nextURI = uris[0]; | ||
uris.splice(0, 1); | ||
if(uris.length == 0) | ||
{ | ||
currentURI = null; | ||
return; | ||
} | ||
currentURI = nextURI; | ||
tryQueuePlayback(); | ||
} | ||
|
||
Queue.skipToTrack = function(index) | ||
{ | ||
uris.splice(0, index); | ||
Queue.nextTrack(); | ||
} | ||
|
||
Queue.addTrack = function(uri) | ||
{ | ||
const uriParts = SpotifyURI.parse(uri); | ||
if(type !== 'track') | ||
{ | ||
throw new Error("invalid URI: must be a track"); | ||
} | ||
var wasEmpty = (uris.length == 0); | ||
uris.push(uri); | ||
} | ||
|
||
Queue.removeTrack = function(index) | ||
{ | ||
if(!Number.isInteger(index)) | ||
{ | ||
throw new Error("invalid index"); | ||
} | ||
uris.splice(index, 1); | ||
} | ||
|
||
Queue.moveTrack = function(sourceIndex, destIndex) | ||
{ | ||
if(!Number.isInteger(sourceIndex) || !Number.isInteger(destIndex) | ||
|| sourceIndex < 0 || sourceIndex >= uris.length | ||
|| destIndex < 0 || destIndex >= uris.length) | ||
{ | ||
throw new Error("invalid index"); | ||
} | ||
var uri = uris[sourceIndex]; | ||
uris.splice(sourceIndex, 1); | ||
uris.splice(destIndex, 0, uri); | ||
} | ||
|
||
Queue.getURIs = function() | ||
{ | ||
return uris.slice(0); | ||
} | ||
|
||
Queue.clear = function() | ||
{ | ||
currentURI = null; | ||
uris = []; | ||
} | ||
|
||
export default Queue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
import { NativeModules } from 'react-native'; | ||
import NativeModuleEvents from 'react-native-events'; | ||
|
||
const Spotify = NativeModules.Spotify; | ||
NativeModuleEvents.registerNativeModule(Spotify); | ||
|
||
export default Spotify; |