Skip to content

Commit c79cfba

Browse files
committed
feat: Player RESET event
1 parent cc329c4 commit c79cfba

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

packages/app/src/components/PlayerControls.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ function Seekbar() {
4646
onSeeked: seekTo,
4747
});
4848

49-
const percentage = (time - seekableStart) / (duration - seekableStart);
49+
let percentage = Number.isNaN(duration)
50+
? 0
51+
: (time - seekableStart) / (duration - seekableStart);
52+
if (percentage < 0) {
53+
percentage = 0;
54+
}
5055

5156
return (
5257
<div {...seekbar.rootProps} className="w-full relative cursor-pointer">

packages/player/src/hls-player.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { EventEmitter } from "tseep";
44
import { EventManager } from "./event-manager";
55
import { getLangCode } from "./helpers";
66
import { getState, State } from "./state";
7+
import { Events } from "./types";
78
import type {
89
AudioTrack,
910
HlsPlayerEventMap,
@@ -25,9 +26,6 @@ export class HlsPlayer {
2526

2627
constructor(public container: HTMLDivElement) {
2728
this.media_ = this.createMedia_();
28-
29-
// Make sure we're in unload state.
30-
this.unload();
3129
}
3230

3331
private createMedia_() {
@@ -43,11 +41,13 @@ export class HlsPlayer {
4341
}
4442

4543
load(url: string) {
44+
this.unload();
45+
4646
this.bindMediaListeners_();
4747
const hls = this.createHls_();
4848

4949
this.state_ = new State({
50-
emitter: this.emitter_,
50+
onEvent: (event: Events) => this.emit_(event),
5151
getTiming: () => ({
5252
primary: hls.interstitialsManager?.primary ?? hls.media,
5353
asset: hls.interstitialsManager?.playerQueue.find(
@@ -71,6 +71,8 @@ export class HlsPlayer {
7171
this.hls_.destroy();
7272
this.hls_ = null;
7373
}
74+
75+
this.emit_(Events.RESET);
7476
}
7577

7678
destroy() {
@@ -417,4 +419,9 @@ export class HlsPlayer {
417419
this.state_?.setSeeking(false);
418420
});
419421
}
422+
423+
private emit_(event: Events) {
424+
this.emitter_.emit(event);
425+
this.emitter_.emit("*", event);
426+
}
420427
}

packages/player/src/state.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ import { Events } from "./types";
33
import type {
44
Asset,
55
AudioTrack,
6-
HlsPlayerEventMap,
76
Interstitial,
87
Playhead,
98
Quality,
109
SubtitleTrack,
1110
} from "./types";
12-
import type { EventEmitter } from "tseep";
1311

1412
interface MediaShim {
1513
currentTime: number;
1614
duration: number;
1715
}
1816

1917
interface StateParams {
20-
emitter: EventEmitter<HlsPlayerEventMap>;
18+
onEvent(event: Events): void;
2119
getTiming(): {
2220
primary?: MediaShim | null;
2321
asset?: MediaShim | null;
@@ -69,7 +67,7 @@ export class State implements StateProperties {
6967
}
7068
this.ready = true;
7169
this.requestTimingSync();
72-
this.emit_(Events.READY);
70+
this.params_.onEvent(Events.READY);
7371
}
7472

7573
setPlayhead(playhead: Playhead) {
@@ -83,20 +81,20 @@ export class State implements StateProperties {
8381
this.requestTimingSync();
8482
}
8583

86-
this.emit_(Events.PLAYHEAD_CHANGE);
84+
this.params_.onEvent(Events.PLAYHEAD_CHANGE);
8785
}
8886

8987
setStarted() {
9088
if (this.started) {
9189
return;
9290
}
9391
this.started = true;
94-
this.emit_(Events.STARTED);
92+
this.params_.onEvent(Events.STARTED);
9593
}
9694

9795
setInterstitial(interstitial: Interstitial | null) {
9896
this.interstitial = interstitial;
99-
this.emit_(Events.INTERSTITIAL_CHANGE);
97+
this.params_.onEvent(Events.INTERSTITIAL_CHANGE);
10098
}
10199

102100
setAsset(asset: Omit<Asset, "time" | "duration"> | null) {
@@ -121,12 +119,12 @@ export class State implements StateProperties {
121119

122120
if (diff(this.qualities) !== diff(qualities)) {
123121
this.qualities = qualities;
124-
this.emit_(Events.QUALITIES_CHANGE);
122+
this.params_.onEvent(Events.QUALITIES_CHANGE);
125123
}
126124

127125
if (autoQuality !== this.autoQuality) {
128126
this.autoQuality = autoQuality;
129-
this.emit_(Events.AUTO_QUALITY_CHANGE);
127+
this.params_.onEvent(Events.AUTO_QUALITY_CHANGE);
130128
}
131129
}
132130

@@ -135,7 +133,7 @@ export class State implements StateProperties {
135133

136134
if (diff(this.audioTracks) !== diff(audioTracks)) {
137135
this.audioTracks = audioTracks;
138-
this.emit_(Events.AUDIO_TRACKS_CHANGE);
136+
this.params_.onEvent(Events.AUDIO_TRACKS_CHANGE);
139137
}
140138
}
141139

@@ -148,7 +146,7 @@ export class State implements StateProperties {
148146
diff(this.subtitleTracks) !== diff(subtitleTracks)
149147
) {
150148
this.subtitleTracks = subtitleTracks;
151-
this.emit_(Events.SUBTITLE_TRACKS_CHANGE);
149+
this.params_.onEvent(Events.SUBTITLE_TRACKS_CHANGE);
152150
}
153151
}
154152

@@ -157,20 +155,20 @@ export class State implements StateProperties {
157155
return;
158156
}
159157
this.volume = volume;
160-
this.emit_(Events.VOLUME_CHANGE);
158+
this.params_.onEvent(Events.VOLUME_CHANGE);
161159
}
162160

163161
setSeeking(seeking: boolean) {
164162
if (seeking === this.seeking) {
165163
return;
166164
}
167165
this.seeking = seeking;
168-
this.emit_(Events.SEEKING_CHANGE);
166+
this.params_.onEvent(Events.SEEKING_CHANGE);
169167
}
170168

171169
setCuePoints(cuePoints: number[]) {
172170
this.cuePoints = cuePoints;
173-
this.emit_(Events.CUEPOINTS_CHANGE);
171+
this.params_.onEvent(Events.CUEPOINTS_CHANGE);
174172
}
175173

176174
requestTimingSync() {
@@ -195,7 +193,7 @@ export class State implements StateProperties {
195193
}
196194

197195
if (shouldEmit) {
198-
this.emit_(Events.TIME_CHANGE);
196+
this.params_.onEvent(Events.TIME_CHANGE);
199197
}
200198
}
201199

@@ -227,11 +225,6 @@ export class State implements StateProperties {
227225
return oldTime !== target.time || oldDuration !== target.duration;
228226
}
229227

230-
private emit_(event: Events) {
231-
this.params_.emitter.emit(event);
232-
this.params_.emitter.emit("*", event);
233-
}
234-
235228
ready = noState.ready;
236229
playhead = noState.playhead;
237230
started = noState.started;

packages/player/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Level, MediaPlaylist } from "hls.js";
33
export type Playhead = "idle" | "play" | "playing" | "pause" | "ended";
44

55
export enum Events {
6+
RESET = "reset",
67
READY = "ready",
78
STARTED = "started",
89
PLAYHEAD_CHANGE = "playheadChange",
@@ -18,6 +19,7 @@ export enum Events {
1819
}
1920

2021
export type HlsPlayerEventMap = {
22+
[Events.RESET]: () => void;
2123
[Events.READY]: () => void;
2224
[Events.STARTED]: () => void;
2325
[Events.PLAYHEAD_CHANGE]: () => void;

0 commit comments

Comments
 (0)