Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import {
Component, Input, OnInit
} from '@angular/core';
import { MatSliderChange } from '@angular/material/slider';
@Component({
selector: 'app-control-bar',
templateUrl: './control-bar.component.html',
styleUrls: ['./control-bar.component.css']
})
export class ControlBarComponent implements OnInit {
@Input() player!: HTMLVideoElement | HTMLAudioElement;
duration!: number;
currentTime!: number;
playing!: boolean;
pausing!: boolean;
runCounter!: number;
lastVolume!: number;
ngOnInit(): void {
// Firefox has problems to get the duration
this.player.ondurationchange = () => this.getDuration();
this.player.onloadedmetadata = () => this.getDuration();
this.player.onloadeddata = () => this.getDuration();
this.player.onprogress = () => this.getDuration();
this.player.oncanplay = () => this.getDuration();
this.player.oncanplaythrough = () => this.getDuration();
this.player.ontimeupdate = () => { this.currentTime = this.player.currentTime / 60; };
this.player.onpause = () => { this.playing = false; this.pausing = true; };
this.player.onended = () => { this.runCounter += 1; }; // playing, pausing?
this.player.onvolumechange = () => { this.player.muted = !this.player.volume; };
this.lastVolume = this.player.volume;
}
play(): void {
// eslint-disable-next-line no-console
this.player.play().then(() => { this.playing = true; this.pausing = false; }, () => console.error('error'));
}
pause(): void {
this.player.pause();
}
stop(): void {
this.player.pause();
this.player.currentTime = 0;
}
onTimeChange(event: MatSliderChange): void {
this.player.currentTime = event.value ? event.value : 0;
}
onVolumeChange(event: MatSliderChange): void {
this.player.volume = event.value ? event.value : 0;
}
toggleVolume(): void {
if (this.player.volume) {
this.lastVolume = this.player.volume;
this.player.volume = 0;
} else {
this.player.volume = this.lastVolume;
}
}
private getDuration(): void {
if (this.player.duration !== Infinity && this.player.duration && !this.duration) {
this.duration = this.player.duration / 60;
} else {
this.duration = 0;
}
}
}