Skip to content

Commit

Permalink
Add switch track
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaggers committed Aug 8, 2017
1 parent f0e4dbb commit 7a810bc
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Examples/Basics/PlayPage.ux
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<StackPanel>
<Each Items="{tracks}">
<DockPanel Padding="10" Margin="0,1" Background="#fff">
<Text Value="{name}" />
<Text Value="{name}" Clicked="{itemClicked}"/>
</DockPanel>
</Each>
</StackPanel>
Expand Down
9 changes: 8 additions & 1 deletion Examples/Basics/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ var paramObs = this.Parameter.onValueChanged(module, function(param) {
"duration": track["duration"]
};
});
tracks.addAll(tmp);
StreamingPlayer.playlist = tmp;
tracks.addAll(StreamingPlayer.playlist);
}).catch(function(e) {
console.log("Well damn:" + e);
});
});

var itemClicked = function(item) {
var track = item["data"];
console.log("Foo?: " + JSON.stringify(track));
StreamingPlayer.switchTrack(track);
};

var backClicked = function() {
StreamingPlayer.stop();
router.goto("artists");
Expand All @@ -45,6 +51,7 @@ module.exports = {
backClicked: backClicked,
playClicked: StreamingPlayer.play,
pauseClicked: StreamingPlayer.pause,
itemClicked: itemClicked,

tracks: tracks
};
50 changes: 49 additions & 1 deletion src/Android/StreamingAudioService.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ private void DropFuture()
}
}

private int SetCurrentPlaylistTrack(int trackUID)
{

int index = _trackPlaylist.indexOf(trackUID);
if (index > -1)
{
// If we were playing from history then we dont want to push the current
// track to history as it is already there.
boolean wasntPlayingFromHistory = _trackHistoryCurrentIndex == -1;

// If we were in the history then moving structurally starts making a new
// history. This means we drop the future.
DropFuture();

if (wasntPlayingFromHistory)
{
PushCurrentToHistory();
}

_trackPlaylistCurrentIndex = index;
return trackUID;
}
else
{
return -1;
}
}

private int MoveToNextPlaylistTrack()
{
int uid = PlaylistNextTrackUID();
Expand Down Expand Up @@ -443,6 +471,11 @@ else if (action.equals("Backward"))
{
Backward();
}
else if (action.equals("SwitchTrack"))
{
int uid = extras.getInt("uid");
SwitchTrack(uid);
}
}
});

Expand Down Expand Up @@ -667,6 +700,15 @@ private void Stop()
MakeTrackCurrentByUID(-1);
}

private void SwitchTrack(int uid)
{
int validatedUID = SetCurrentPlaylistTrack(uid);
if (validatedUID > -1)
{
MakeTrackCurrentByUID(validatedUID);
}
}

//
// Events
//
Expand Down Expand Up @@ -753,7 +795,6 @@ public IBinder onBind(Intent intent)
return _binder;
}


@Override
public boolean onUnbind(Intent intent)
{
Expand Down Expand Up @@ -817,6 +858,13 @@ public final void Seek(long position)
_controller.getTransportControls().seekTo(position);
}

public final void SwitchTrack(int uid)
{
Bundle bTrack = new Bundle();
bTrack.putInt("uid", uid);
_controller.getTransportControls().sendCustomAction("SwitchTrack", bTrack);
}

public final double GetCurrentPosition()
{
if (_lastPlayerState == null)
Expand Down
15 changes: 15 additions & 0 deletions src/Android/StreamingPlayer.uno
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,14 @@ namespace StreamingPlayer
}
}

static public void SwitchTrack(Track track)
{
if (IsConnected)
{
SwitchTrackImpl(_client, track.UID);
}
}

[Foreign(Language.Java)]
static void NextImpl(Java.Object client)
@{
Expand Down Expand Up @@ -467,5 +475,12 @@ namespace StreamingPlayer
StreamingAudioService.StreamingAudioClient sClient = (StreamingAudioService.StreamingAudioClient)client;
sClient.Forward();
@}

[Foreign(Language.Java)]
static void SwitchTrackImpl(Java.Object client, int uid)
@{
StreamingAudioService.StreamingAudioClient sClient = (StreamingAudioService.StreamingAudioClient)client;
sClient.SwitchTrack(uid);
@}
}
}
1 change: 1 addition & 0 deletions src/Dummy/StreamingPlayerDummyImpl.uno
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ namespace StreamingPlayer
static public void Next() {}
static public void Backward() {}
static public void Forward() {}
static public void SwitchTrack(Track track) {}
}
}
9 changes: 9 additions & 0 deletions src/JS.uno
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace StreamingPlayer
AddMember(new NativeFunction("pause", (NativeCallback)Pause));
AddMember(new NativeFunction("stop", (NativeCallback)Stop));
AddMember(new NativeFunction("seek", (NativeCallback)Seek));
AddMember(new NativeFunction("switchTrack", (NativeCallback)SwitchTrack));

AddMember(new NativeProperty<PlayerStatus,string>("status", GetStatus, null, PlayerStatusConverter.Convert));
AddMember(new NativeProperty<double,double>("duration", GetDuration));
Expand Down Expand Up @@ -80,6 +81,14 @@ namespace StreamingPlayer
Emit("statusChanged", status.Stringify());
}

public object SwitchTrack(Context c, object[] args)
{
if (!_playerInitialized) return null;
var track = Marshal.ToType<Track>(args[0]);
StreamingPlayer.SwitchTrack(track);
return null;
}

public object Next(Context c, object[] args)
{
if (!_playerInitialized) return null;
Expand Down
28 changes: 28 additions & 0 deletions src/iOS/Playlist.uno
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,33 @@ namespace StreamingPlayer

SetPlaylistCurrent(currentTrackUID);
}

public static int SetCurrentPlaylistTrack(int trackUID)
{

int index = _trackPlaylist.IndexOf(trackUID);
if (index > -1)
{
// If we were playing from history then we dont want to push the current
// track to history as it is already there.
bool wasntPlayingFromHistory = _trackHistoryCurrentIndex == -1;

// If we were in the history then moving structurally starts making a new
// history. This means we drop the future.
DropFuture();

if (wasntPlayingFromHistory)
{
PushCurrentToHistory();
}

_trackPlaylistCurrentIndex = index;
return trackUID;
}
else
{
return -1;
}
}
}
}
9 changes: 9 additions & 0 deletions src/iOS/StreamingPlayer.uno
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ namespace StreamingPlayer
}
}

static public void SwitchTrack(Track track)
{
var uid = Playlist.SetCurrentPlaylistTrack(track.UID);
if (uid > -1)
{
MakeTrackCurrentByUID(uid);
}
}

static public void Seek(double toProgress)
{
if (Status == PlayerStatus.Loading) return;
Expand Down
4 changes: 2 additions & 2 deletions todo
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

- add clear history

- add 'set current track'

## Done

I: Forward works even when no history
Expand Down Expand Up @@ -117,3 +115,5 @@ R: backward could return -1 and we would then set this as the current track (mea
I: fix get playlist
R: -

I: add 'set current track'
R: -

0 comments on commit 7a810bc

Please sign in to comment.