@@ -285,17 +285,19 @@ private void updateTracks(@NotNull Remote3Frame frame) {
285285 stateUpdated ();
286286 }
287287
288+ private void loadTracksProvider (@ NotNull String context ) {
289+ if (context .startsWith ("spotify:station:" ) || context .startsWith ("spotify:dailymix:" ))
290+ tracksProvider = new StationProvider (session , state .state );
291+ else if (context .startsWith ("spotify:show:" ) || context .startsWith ("spotify:episode:" ))
292+ tracksProvider = new ShowProvider (state .state );
293+ else
294+ tracksProvider = new PlaylistProvider (session , state .state , conf );
295+ }
296+
288297 private void updateContext (@ NotNull Remote3Frame frame ) {
289298 if (frame .context .uri != null ) {
290299 state .updateContext (frame );
291-
292- String context = frame .context .uri ;
293- if (context .startsWith ("spotify:station:" ) || context .startsWith ("spotify:dailymix:" ))
294- tracksProvider = new StationProvider (session , state .state );
295- else if (context .startsWith ("spotify:show:" ) || context .startsWith ("spotify:episode:" ))
296- tracksProvider = new ShowProvider (state .state );
297- else
298- tracksProvider = new PlaylistProvider (session , state .state , conf );
300+ loadTracksProvider (frame .context .uri );
299301 }
300302
301303 if (frame .options != null && frame .options .playerOptionsOverride != null ) {
@@ -525,17 +527,24 @@ private void loadAutoplay() {
525527 }
526528
527529 try {
528- MercuryRequests .StationsWrapper json = session .mercury ().sendSync (MercuryRequests .getStationFor (context ));
529- state .updateContext (json );
530+ MercuryClient .Response resp = session .mercury ().sendSync (MercuryRequests .autoplayQuery (context ));
531+ if (resp .statusCode == 200 ) {
532+ String newContext = resp .payload .readIntoString (0 );
533+ state .loadStationContext (newContext );
530534
531- state .setPositionMs (0 );
532- state .setPositionMeasuredAt (TimeProvider .currentTimeMillis ());
535+ state .setPositionMs (0 );
536+ state .setPositionMeasuredAt (TimeProvider .currentTimeMillis ());
533537
534- tracksProvider = new StationProvider (session , state .state );
535- loadTrack (true );
538+ tracksProvider = new StationProvider (session , state .state );
539+ loadTrack (true );
536540
537- LOGGER .debug (String .format ("Loading context for autoplay, uri: %s" , json .uri ()));
538- } catch (IOException | MercuryClient .MercuryException ex ) {
541+ LOGGER .debug (String .format ("Loading context for autoplay, uri: %s" , newContext ));
542+ } else {
543+ LOGGER .fatal ("Failed retrieving autoplay context, code: " + resp .statusCode );
544+ state .setStatus (Spirc .PlayStatus .kPlayStatusStop );
545+ stateUpdated ();
546+ }
547+ } catch (IOException ex ) {
539548 LOGGER .fatal ("Failed loading autoplay station!" , ex );
540549 state .setStatus (Spirc .PlayStatus .kPlayStatusStop );
541550 stateUpdated ();
@@ -789,12 +798,20 @@ void updateContext(@NotNull Remote3Frame frame) {
789798 }
790799 }
791800
792- void updateContext (@ NotNull MercuryRequests .StationsWrapper json ) {
793- state .setContextUri (json .uri ());
801+ void loadStationContext (@ NotNull String context ) {
802+ loadTracksProvider (context );
803+ if (!(tracksProvider instanceof StationProvider )) {
804+ LOGGER .fatal ("Not a station context: " + context );
805+ setStatus (Spirc .PlayStatus .kPlayStatusStop );
806+ stateUpdated ();
807+ return ;
808+ }
809+
810+ state .setContextUri (context );
794811
795- state .setPlayingTrackIndex (0 );
796812 state .clearTrack ();
797- state .addAllTrack (json .tracks ());
813+ state .setPlayingTrackIndex (-1 ); // A bit hacky but works
814+ state .setPlayingTrackIndex (tracksProvider .getNextTrackIndex (false ));
798815 }
799816
800817 long getPositionMeasuredAt () {
@@ -828,7 +845,5 @@ void setPlayingTrackIndex(int i) {
828845 int getTrackCount () {
829846 return state .getTrackCount ();
830847 }
831-
832-
833848 }
834849}
0 commit comments