Skip to content

Instantly share code, notes, and snippets.

@seresk
Created April 19, 2021 21:02
Show Gist options
  • Save seresk/b94e341670bd982db0766d025cb51884 to your computer and use it in GitHub Desktop.
Save seresk/b94e341670bd982db0766d025cb51884 to your computer and use it in GitHub Desktop.
AppleMusicPlayBehavior partial implementation
export class AppleMusicPlayBehavior implements IPlayBehaviour {
public TAG: string;
public playState: PlayStateEnum;
public playedMs: number;
public playlist: NowPlayingPlaylistModel = new NowPlayingPlaylistModel();
public progress: number;
public trackPlayedLabel: string;
public trackRemainingLabel: string;
public volume: number;
musicKit: MusicKitInstance;
constructor(private streamingService: StreamingService, private publisher: PublisherService) {
this.musicKit = AppleMusicKit.getInstance();
}
seek(seekToMs: number): void {
this.isSeeking = true;
if (seekToMs < 0) {
seekToMs = 0;
}
if (seekToMs > this.playlist.currentTrack.durationMs) {
return;
}
this.playlist.progressMs = seekToMs;
this.playedMs = this.playlist.progressMs;
this.progress = Math.min(this.playedMs / Math.floor(this.playlist.currentTrack.durationMs) * 100, 100);
const playbackTimeDidChangeModel = new PlaybackTimeDidChangeModel();
playbackTimeDidChangeModel.currentPlaybackDuration = this.playlist.progressMs / 1000;
playbackTimeDidChangeModel.currentPlaybackTime = this.playlist.currentTrack.durationMs / 1000;
playbackTimeDidChangeModel.currentPlaybackTimeRemaining = (this.playlist.currentTrack.durationMs - this.playlist.progressMs) / 1000;
this.setPlaybackLabels(playbackTimeDidChangeModel);
this.clearSeekRequestTimeout();
this.setSeekRequestTimeout(seekToMs);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment