Skip to content

Commit

Permalink
Functional draft progress bar next to play button for current playing…
Browse files Browse the repository at this point in the history
… track.

It can be interacted with but the slider behaves incorrectly when the user *slides it* instead of simply clicking somewhere on the bar : this is due to the fact that the value of the slider is updated by querying player API
  • Loading branch information
anavarr committed Jun 4, 2024
1 parent 4dc0295 commit 38f6565
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
55 changes: 55 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export default class SoundscapesPlugin extends Plugin {
playButton: HTMLButtonElement;
pauseButton: HTMLButtonElement;
nextButton: HTMLButtonElement;
trackProgressSlider: HTMLInputElement;
progressDuration : HTMLDivElement;
previousButton: HTMLButtonElement;
changeSoundscapeSelect: HTMLSelectElement;
nowPlayingRoot: HTMLDivElement;
Expand Down Expand Up @@ -366,6 +368,29 @@ export default class SoundscapesPlugin extends Plugin {
setIcon(this.nextButton, "skip-forward");
this.nextButton.onclick = () => this.next();

// Progress : Duration
this.progressDuration = this.statusBarItem.createEl("div", {
cls: "soundscapesroot-progress-duration",
});
// Change Button
this.trackProgressSlider = this.statusBarItem.createEl("input", {
attr: {
type: "range",
min: 0,
max: 1,
step: 0.01,
value: this.settings.trackProgress,
class : "trackProgress"
},
});
// Create a virtual event object
this.onTrackProgressChange({ target: { value: this.settings.trackProgress } });

this.trackProgressSlider.addEventListener(
"input",
this.onTrackProgressChange.bind(this)
)

// Change Soundscape Button
const changeSoundscapeButton = this.statusBarItem.createEl("button", {
cls: "soundscapesroot-changesoundscapebutton",
Expand Down Expand Up @@ -479,6 +504,7 @@ export default class SoundscapesPlugin extends Plugin {
* Plays the current track. When it's a live video, attempt to jump to the "live" portion.
*/
play() {
var self = this;
if (
this.soundscapeType === SOUNDSCAPE_TYPE.STANDARD &&
SOUNDSCAPES[this.settings.soundscape].isLiveVideo
Expand Down Expand Up @@ -613,6 +639,9 @@ export default class SoundscapesPlugin extends Plugin {
seek(time: number) {
if (this.soundscapeType === SOUNDSCAPE_TYPE.MY_MUSIC) {
this.localPlayer.currentTime = time;
} else {
var t= this.player?.getCurrentTime();
this.player?.seekTo(t+time);
}
}

Expand Down Expand Up @@ -691,8 +720,26 @@ export default class SoundscapesPlugin extends Plugin {
* Once the player is ready, create the controls and play some music! (or not if autoplay is disabled)
*/
onPlayerReady() {
var self = this
this.createControls();
this.onSoundscapeChange(this.settings.autoplay);
setInterval(function() {
var progress = (self.player?.getCurrentTime()/self.player?.getDuration()).toFixed(5)
self.trackProgressSlider.value = (progress).toString()
self.progressDuration.setText(
(self.formatDate(self.player?.getCurrentTime())+" / "+self.formatDate(self.player?.getDuration()))
)
}, 10);
}

formatDate(duration: number){
if(duration < 3600){
return new Date(duration*1000).toISOString().substring(14, 19)
}else if(duration <3600*24){
return new Date(duration*1000).toISOString().substring(11, 19)
}else{
return Math.round(duration/(3600*24)) +":"+ new Date(duration*1000).toISOString().substring(11, 19)
}
}

/**
Expand Down Expand Up @@ -777,6 +824,14 @@ export default class SoundscapesPlugin extends Plugin {
this.debouncedSaveSettings();
}

onTrackProgressChange(e: any) {
const trackProgress = parseFloat(e.target.value);
this.trackProgressSlider.value = e.target.value;
this.player?.seekTo(this.player.getDuration()*(trackProgress));
this.settings.trackProgress = trackProgress;
this.settingsObservable.setValue(this.settings);
this.debouncedSaveSettings();
}
/**
* Play the selected soundscape!
*
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface SoundscapesPluginSettings {
autoplay: boolean;
scrollSongTitle: boolean;
customSoundscapes: CustomSoundscape[];
trackProgress: number;
myMusicIndex: LocalMusicFile[];
myMusicFolderPath: string;
reindexFrequency: string;
Expand All @@ -26,6 +27,7 @@ export const DEFAULT_SETTINGS: SoundscapesPluginSettings = {
volume: 25,
autoplay: false,
scrollSongTitle: true,
trackProgress: 0,
customSoundscapes: [],
myMusicIndex: [],
myMusicFolderPath: "",
Expand Down
1 change: 1 addition & 0 deletions src/Types/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface Player {
getDuration(): number;
setVolume(volume: Number): void;
loadVideoById(options: { videoId: String | undefined }): void;
getCurrentTime():number;
}

export interface LocalMusicFile {
Expand Down
10 changes: 10 additions & 0 deletions styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
right: 0;
}

.soundscapesroot-progress-duration{
margin: 10px;
width: auto;
white-space: nowrap;
}

.soundscapesroot-nowplaying {
margin: 0 12px;
width: 150px;
Expand All @@ -79,6 +85,10 @@
}
}
}

.trackProgress{
width: 100%;
}
}

input.soundscapes-validation-error {
Expand Down

0 comments on commit 38f6565

Please sign in to comment.