66import org .apache .log4j .Logger ;
77import org .jetbrains .annotations .NotNull ;
88import xyz .gianlu .librespot .common .Utils ;
9- import xyz .gianlu .librespot .common .proto .Metadata ;
109import xyz .gianlu .librespot .common .proto .Spirc ;
1110import xyz .gianlu .librespot .core .Session ;
1211import xyz .gianlu .librespot .mercury .MercuryClient ;
2019import java .util .Collections ;
2120import java .util .Iterator ;
2221import java .util .List ;
23- import java .util .concurrent .Executors ;
24- import java .util .concurrent .ScheduledExecutorService ;
25- import java .util .concurrent .TimeUnit ;
2622
2723/**
2824 * @author Gianlu
2925 */
3026public class Player implements FrameListener , TrackHandler .Listener {
3127 private static final Logger LOGGER = Logger .getLogger (Player .class );
32- private static final long TRACK_PRELOAD_THRESHOLD = TimeUnit . SECONDS . toMillis ( 10 );
28+
3329 private final Session session ;
3430 private final SpotifyIrc spirc ;
3531 private final Spirc .State .Builder state ;
3632 private final PlayerConfiguration conf ;
3733 private final CacheManager cacheManager ;
38- private final PreloadScheduler scheduler = new PreloadScheduler ();
3934 private TrackHandler trackHandler ;
4035 private TrackHandler preloadTrackHandler ;
4136
@@ -234,8 +229,6 @@ private void handleSeek(int pos) {
234229 state .setPositionMeasuredAt (System .currentTimeMillis ());
235230 if (trackHandler != null ) trackHandler .sendSeek (pos );
236231 stateUpdated ();
237-
238- scheduler .reschedule ();
239232 }
240233
241234 private void updatedTracks (@ NotNull Spirc .Frame frame ) {
@@ -278,12 +271,15 @@ public void endOfTrack(@NotNull TrackHandler handler) {
278271 }
279272 }
280273
281- private void preloadNextTrack () {
282- Spirc .TrackRef next = state .getTrack (getQueuedTrack (false ));
274+ @ Override
275+ public void preloadNextTrack (@ NotNull TrackHandler handler ) {
276+ if (handler == trackHandler ) {
277+ Spirc .TrackRef next = state .getTrack (getQueuedTrack (false ));
283278
284- preloadTrackHandler = new TrackHandler (session , cacheManager , conf , this );
285- preloadTrackHandler .sendLoad (next , false , 0 );
286- LOGGER .trace ("Started next track preload, gid: " + Utils .bytesToHex (next .getGid ()));
279+ preloadTrackHandler = new TrackHandler (session , cacheManager , conf , this );
280+ preloadTrackHandler .sendLoad (next , false , 0 );
281+ LOGGER .trace ("Started next track preload, gid: " + Utils .bytesToHex (next .getGid ()));
282+ }
287283 }
288284
289285 private void handleLoad (@ NotNull Spirc .Frame frame ) {
@@ -328,8 +324,6 @@ private void loadTrack(boolean play) {
328324 }
329325
330326 stateUpdated ();
331-
332- scheduler .reschedule ();
333327 }
334328
335329 private void handlePlay () {
@@ -338,8 +332,6 @@ private void handlePlay() {
338332 state .setStatus (Spirc .PlayStatus .kPlayStatusPlay );
339333 state .setPositionMeasuredAt (System .currentTimeMillis ());
340334 stateUpdated ();
341-
342- scheduler .reschedule ();
343335 }
344336 }
345337
@@ -354,8 +346,6 @@ private void handlePause() {
354346 state .setPositionMs (pos + diff );
355347 state .setPositionMeasuredAt (now );
356348 stateUpdated ();
357-
358- scheduler .reschedule ();
359349 }
360350 }
361351
@@ -405,8 +395,6 @@ private void handlePrev() {
405395 state .setPositionMeasuredAt (System .currentTimeMillis ());
406396 if (trackHandler != null ) trackHandler .sendSeek (0 );
407397 stateUpdated ();
408-
409- scheduler .reschedule ();
410398 }
411399 }
412400
@@ -428,33 +416,4 @@ public interface PlayerConfiguration {
428416
429417 float normalisationPregain ();
430418 }
431-
432- private class PreloadScheduler {
433- private final ScheduledExecutorService executor = Executors .newSingleThreadScheduledExecutor ();
434- private Task activeTask ;
435-
436- void reschedule () {
437- if (activeTask != null ) activeTask .abort ();
438-
439- if (!conf .preloadEnabled () || state .getStatus () != Spirc .PlayStatus .kPlayStatusPlay ) return ;
440- Metadata .Track track = trackHandler != null ? trackHandler .track () : null ;
441- if (track == null ) return ;
442-
443- activeTask = new Task ();
444- executor .schedule (activeTask , (track .getDuration () - getPosition ()) - TRACK_PRELOAD_THRESHOLD , TimeUnit .MILLISECONDS );
445- }
446-
447- private class Task implements Runnable {
448- private volatile boolean aborted = false ;
449-
450- @ Override
451- public void run () {
452- if (!aborted ) preloadNextTrack ();
453- }
454-
455- private void abort () {
456- aborted = true ;
457- }
458- }
459- }
460419}
0 commit comments