-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx-mini-audio-player.ts
58 lines (52 loc) · 1.49 KB
/
ngx-mini-audio-player.ts
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
/**
* (c) 2010-2020 Diego Pereira. https://github.com/diegodsp/ngx-mini-audio-player/
* License: MIT
*/
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'ngx-mini-audio-player',
template: '<i class="fa" [class.fa-download]="buffering" [class.fa-play]="!buffering && !playing" [class.fa-pause]="!buffering && playing" (click)="onClick()"></i>'
})
export class MiniAudioPlayerComponent implements OnInit, OnDestroy {
player = new Audio();
playing = false;
buffering = false;
@Input() url: string;
constructor(private route: ActivatedRoute) { }
/**
* Handle events.
*/
ngOnInit(): void {
this.route.queryParams.subscribe(params => {
this.ngOnDestroy();
});
this.player.onloadstart = () => {
this.buffering = true;
};
this.player.onloadeddata = () => {
this.buffering = false;
};
}
/**
* Detect change pages or recreate component.
*/
ngOnDestroy(): void {
this.buffering = false;
this.playing = false;
this.player?.pause();
}
/**
* Handle the click to play/pause the audio.
*/
onClick() {
if (this.playing) {
this.player.pause();
} else {
this.player.src = this.url;
this.player.load();
this.player.play();
}
this.playing = !this.playing;
}
}