Skip to content

Commit

Permalink
Play doesnt take a track, always acts on CurrentTrack
Browse files Browse the repository at this point in the history
Also tried to gather iOS state to make sense of it. What a mess
  • Loading branch information
cbaggers committed Jun 9, 2017
1 parent 16bef16 commit a99ba24
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 141 deletions.
40 changes: 20 additions & 20 deletions src/Android/StreamingAudioService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.os.IBinder;
import android.os.RemoteException;
import android.os.SystemClock;
import android.support.v4.app.BundleCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaButtonReceiver;
Expand Down Expand Up @@ -48,20 +47,26 @@ public final class StreamingAudioService
// Uno interaction
StreamingAudioClient _streamingAudioClient;
ArrayList<Track> _playlist = new ArrayList<Track>();
Track _currentTrack;
int _currentTrackIndex = -1;

private Track GetCurrentTrack()
{
return _currentTrack;
return _playlist.get(_currentTrackIndex);
}
private void SetCurrentTrack(Track track)

private void SetCurrentTrackIndex(int index)
{
if (_currentTrack != track)
if (_currentTrackIndex != index)
{
_currentTrack = track;
_currentTrackIndex = index;
Bundle extras = new Bundle();
extras.putDouble("duration", track.Duration);
extras.putInt("index", CurrentTrackIndex());
extras.putInt("index", index);

if (index == -1)
extras.putDouble("duration", 0);
else
extras.putDouble("duration", _playlist.get(index).Duration);

_session.sendSessionEvent("trackChanged", extras);
}
}
Expand Down Expand Up @@ -166,10 +171,6 @@ public void onCustomAction(String action, Bundle extras)
{
Resume();
}
else if (action.equals("PlayTrack"))
{
Play((Track) extras.getParcelable("track"));
}
else if (action.equals("SetPlaylist"))
{
SetPlaylist((Track[])extras.getParcelableArray("tracks"));
Expand Down Expand Up @@ -309,7 +310,7 @@ private void stopNoisyReciever()
// Actions
//

private void Play(Track track)
private void Play(int index)
{
if (_prepared)
{
Expand All @@ -322,6 +323,7 @@ private void Play(Track track)
_player.reset();
_state = AndroidPlayerState.Initialized;
Logger.Log("SetDataSource: state: " + _state);
Track track = _playlist.get(index);
_player.setDataSource(track.Url);
setPlaybackState(PlaybackStateCompat.STATE_BUFFERING, 0);
_player.prepareAsync();
Expand All @@ -331,7 +333,7 @@ private void Play(Track track)
Logger.Log("Exception while setting MediaPlayer DataSource");
}

SetCurrentTrack(track);
SetCurrentTrackIndex(index);
}

private void Resume()
Expand Down Expand Up @@ -378,15 +380,15 @@ private void Next()
{
if (HasNext())
{
Play(_playlist.get(CurrentTrackIndex() + 1));
Play(CurrentTrackIndex() + 1);
}
}

private void Previous()
{
if (HasPrevious())
{
Play(_playlist.get(CurrentTrackIndex() - 1));
Play(CurrentTrackIndex() - 1);
}
}

Expand Down Expand Up @@ -558,11 +560,9 @@ public StreamingAudioClient(StreamingAudioService service) throws RemoteExceptio
_controller.registerCallback(this);
}

public final void Play(Track track)
public final void Play()
{
Bundle bTrack = new Bundle();
bTrack.putParcelable("track", track);
_controller.getTransportControls().sendCustomAction("PlayTrack", bTrack);
_controller.getTransportControls().play();
}

public final void Resume()
Expand Down
33 changes: 16 additions & 17 deletions src/Android/StreamingPlayerAndroidImpl.uno
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ namespace StreamingPlayer
static Java.Object _serviceConnection; // ServiceConnection
static Java.Object _binder; // StreamingAudioService.LocalBinder
static Java.Object _client;

static bool _initialized;
static bool _permittedToPlay = false;
static PlayerStatus _status = PlayerStatus.Stopped;
static int _currentTrackIndex = -1;
static Track[] _pendingPlaylist;
static bool _pendingPlay = false;

static public bool IsConnected
{
Expand Down Expand Up @@ -62,8 +67,6 @@ namespace StreamingPlayer
//------------------------------------------------------------
// Initializing

static bool _initialized;
static bool _permittedToPlay = false;
static public bool Init()
{
if (!_initialized)
Expand Down Expand Up @@ -224,41 +227,37 @@ namespace StreamingPlayer
return new Track(name, artist, url, artworkUrl, duration);
@}

static bool _pendingPlay = false;
static Track _pendingTrack;
static public void Play(Track track)
static public void Play()
{
if (_service == null)
CreateService();

if (IsConnected)
{
var javaTrack = ToJavaTrack(track.Name, track.Artist, track.Url, track.ArtworkUrl, track.Duration);
Status = PlayerStatus.Loading;
PlayImpl(_client, javaTrack);
PlayImpl(_client);
_pendingPlay = false;
} else {
_pendingPlay = true;
_pendingTrack = track;
}
}

[Foreign(Language.Java)]
static void PlayImpl(Java.Object client, Java.Object track)
static void PlayImpl(Java.Object client)
@{
StreamingAudioService.StreamingAudioClient sClient = (StreamingAudioService.StreamingAudioClient)client;
sClient.Play((Track)track);
sClient.Play();
@}


static void ConnectedToBackgroundService()
{
if (_tempPlaylist != null) {
SetPlaylist(_tempPlaylist);
_tempPlaylist = null;
if (_pendingPlaylist != null) {
SetPlaylist(_pendingPlaylist);
_pendingPlaylist = null;
}
if (_pendingPlay)
Play(_pendingTrack);
Play();
}


Expand Down Expand Up @@ -342,7 +341,7 @@ namespace StreamingPlayer
@}


static Track[] _tempPlaylist;

//int id, string name, string url, string artworkUrl, double duration
static public void SetPlaylist(Track[] tracks)
{
Expand All @@ -368,8 +367,8 @@ namespace StreamingPlayer
debug_log("Android: set current playlist");
SetPlaylistImpl(_client, names, artists, urls, artworkUrls, durations);
} else {
debug_log("Android: caching as _tempPlaylist");
_tempPlaylist = tracks;
debug_log("Android: caching as _pendingPlaylist");
_pendingPlaylist = tracks;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Dummy/StreamingPlayerDummyImpl.uno
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace StreamingPlayer

static public bool Init() { return true; }
static public void SetPlaylist(Track[] tracks) {}
static public void Play(Track track) {}
static public void Play() {}
static public void Resume() {}
static public void Pause() {}
static public void Stop() {}
Expand Down
5 changes: 1 addition & 4 deletions src/JS.uno
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@ namespace StreamingPlayer

object Play(Context c, object[] args)
{
var track = args.ValueOrDefault<Track>(0, null);
if (track == null)
throw new Exception("Play needs a {name,url,streamUrl} argument");
StreamingPlayer.Play(track);
StreamingPlayer.Play();
return null;
}

Expand Down
Loading

0 comments on commit a99ba24

Please sign in to comment.