Browse Source

Add a preload event to warn about new track coming soon

maxthiel 3 years ago
parent
commit
2f7bf54076
2 changed files with 10 additions and 1 deletions
  1. 6 1
      playback/src/player.rs
  2. 4 0
      src/player_event_handler.rs

+ 6 - 1
playback/src/player.rs

@@ -101,6 +101,10 @@ pub enum PlayerEvent {
         track_id: SpotifyId,
         position_ms: u32,
     },
+    // The player is preloading a track.
+    Preloading {
+        track_id: SpotifyId,
+    },
     // The player is playing a track.
     // This event is issued at the start of playback of whenever the position must be communicated
     // because it is out of sync. This includes:
@@ -173,7 +177,7 @@ impl PlayerEvent {
             | Stopped {
                 play_request_id, ..
             } => Some(*play_request_id),
-            Changed { .. } | VolumeSet { .. } => None,
+            Changed { .. } | Preloading { .. } | VolumeSet { .. } => None,
         }
     }
 }
@@ -799,6 +803,7 @@ impl Future for PlayerInternal {
             {
                 match loader.poll() {
                     Ok(Async::Ready(loaded_track)) => {
+                        self.send_event(PlayerEvent::Preloading { track_id });
                         self.preload = PlayerPreload::Ready {
                             track_id,
                             loaded_track,

+ 4 - 0
src/player_event_handler.rs

@@ -58,6 +58,10 @@ pub fn run_program_on_events(event: PlayerEvent, onevent: &str) -> Option<io::Re
             env_vars.insert("DURATION_MS", duration_ms.to_string());
             env_vars.insert("POSITION_MS", position_ms.to_string());
         }
+        PlayerEvent::Preloading { track_id, .. } => {
+            env_vars.insert("PLAYER_EVENT", "preloading".to_string());
+            env_vars.insert("TRACK_ID", track_id.to_base62());
+        }
         PlayerEvent::VolumeSet { volume } => {
             env_vars.insert("PLAYER_EVENT", "volume_set".to_string());
             env_vars.insert("VOLUME", volume.to_string());