Skip to content

Commit

Permalink
feat(player): add volume control and persistence for video players
Browse files Browse the repository at this point in the history
  • Loading branch information
4gray committed Nov 17, 2024
1 parent 1bbd832 commit 67620f0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
<video
#videoPlayer
id="video-player"
autoplay="true"
controls="true"
muted="muted"
></video>
<video #videoPlayer id="video-player" autoplay="true" controls="true"></video>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Input,
OnChanges,
OnDestroy,
OnInit,
SimpleChanges,
ViewChild,
} from '@angular/core';
Expand All @@ -22,10 +23,11 @@ import { DataService } from '../../../services/data.service';
styleUrls: ['./html-video-player.component.scss'],
standalone: true,
})
export class HtmlVideoPlayerComponent implements OnChanges, OnDestroy {
export class HtmlVideoPlayerComponent implements OnInit, OnChanges, OnDestroy {
/** Channel to play */
@Input() channel: Channel;
dataService: DataService; // Declare the dataService property
@Input() volume = 1;

constructor(dataService: DataService) {
this.dataService = dataService; // Inject the DataService
Expand All @@ -41,6 +43,12 @@ export class HtmlVideoPlayerComponent implements OnChanges, OnDestroy {
/** Captions/subtitles indicator */
@Input() showCaptions!: boolean;

ngOnInit() {
this.videoPlayer.nativeElement.addEventListener('volumechange', () => {
this.onVolumeChange();
});
}

/**
* Listen for component input changes
* @param changes component changes
Expand All @@ -49,6 +57,13 @@ export class HtmlVideoPlayerComponent implements OnChanges, OnDestroy {
if (changes.channel && changes.channel.currentValue) {
this.playChannel(changes.channel.currentValue);
}
if (changes.volume?.currentValue !== undefined) {
console.log(
'Setting HTML5 player volume to:',
changes.volume.currentValue
);
this.videoPlayer.nativeElement.volume = changes.volume.currentValue;
}
}

/**
Expand Down Expand Up @@ -128,9 +143,22 @@ export class HtmlVideoPlayerComponent implements OnChanges, OnDestroy {
}

/**
* Destroy hls instance on component destroy
* Save volume when user changes it
*/
onVolumeChange(): void {
const currentVolume = this.videoPlayer.nativeElement.volume;
console.log('Volume changed to:', currentVolume);
localStorage.setItem('volume', currentVolume.toString());
}

/**
* Destroy hls instance on component destroy and clean up event listener
*/
ngOnDestroy(): void {
this.videoPlayer.nativeElement.removeEventListener(
'volumechange',
this.onVolumeChange
);
if (this.hls) {
this.hls.destroy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
},
],
}"
[volume]="volume"
[class.hide-captions]="!playerSettings.showCaptions"
></app-vjs-player>
/>

<!-- default html player component -->
<app-html-video-player
*ngIf="playerSettings.player === 'html5'"
[channel]="activeChannel"
[volume]="volume"
[showCaptions]="playerSettings.showCaptions"
/>
<div
Expand Down Expand Up @@ -82,12 +84,12 @@
<app-info-overlay
[channel]="activeChannel"
[epgProgram]="epgProgram$ | async"
></app-info-overlay>
/>
</ng-template>
</ng-container>
</mat-drawer-content>
<!-- right sidebar content -->
<mat-drawer position="end" #drawerRight mode="side" disableClose>
<app-epg-list *ngIf="isElectron"></app-epg-list>
<app-epg-list *ngIf="isElectron" />
</mat-drawer>
</mat-drawer-container>
18 changes: 14 additions & 4 deletions src/app/player/components/video-player/video-player.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export class VideoPlayerComponent implements OnInit, OnDestroy {
/** EPG overlay reference */
overlayRef: OverlayRef;

volume = 1;

constructor(
private activatedRoute: ActivatedRoute,
private dataService: DataService,
Expand All @@ -112,7 +114,13 @@ export class VideoPlayerComponent implements OnInit, OnDestroy {
private snackBar: MatSnackBar,
private storage: StorageMap,
private store: Store
) {}
) {
// Initialize volume from localStorage in constructor
const savedVolume = localStorage.getItem('volume');
if (savedVolume !== null) {
this.volume = Number(savedVolume);
}
}

/**
* Sets video player and subscribes to channel list from the store
Expand All @@ -137,9 +145,9 @@ export class VideoPlayerComponent implements OnInit, OnDestroy {
CHANNEL_SET_USER_AGENT,
playlist.userAgent
? {
referer: 'localhost',
userAgent: playlist.userAgent,
}
referer: 'localhost',
userAgent: playlist.userAgent,
}
: {}
);

Expand Down Expand Up @@ -205,6 +213,8 @@ export class VideoPlayerComponent implements OnInit, OnDestroy {
player: settings.player || VideoPlayer.VideoJs,
showCaptions: settings.showCaptions || false,
};
// Don't override volume from settings storage anymore
// as we're using localStorage for volume persistence
}
});
}
Expand Down
21 changes: 19 additions & 2 deletions src/app/player/components/vjs-player/vjs-player.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class VjsPlayerComponent implements OnInit, OnChanges, OnDestroy {
@Input() options: videoJs.PlayerOptions;
/** VideoJs object */
player: videoJs.Player;
@Input() volume = 1;

/**
* Instantiate Video.js on component init
Expand All @@ -42,8 +43,17 @@ export class VjsPlayerComponent implements OnInit, OnChanges, OnDestroy {
...this.options,
autoplay: true,
},
function onPlayerReady() {
this.volume(100);
() => {
console.log(
'Setting VideoJS player initial volume to:',
this.volume
);
this.player.volume(this.volume);

this.player.on('volumechange', () => {
const currentVolume = this.player.volume();
localStorage.setItem('volume', currentVolume.toString());
});
}
);
this.player.hlsQualitySelector({
Expand All @@ -60,6 +70,13 @@ export class VjsPlayerComponent implements OnInit, OnChanges, OnDestroy {
if (changes.options.previousValue) {
this.player.src(changes.options.currentValue.sources[0]);
}
if (changes.volume?.currentValue !== undefined && this.player) {
console.log(
'Setting VideoJS player volume to:',
changes.volume.currentValue
);
this.player.volume(changes.volume.currentValue);
}
}

/**
Expand Down

0 comments on commit 67620f0

Please sign in to comment.