forked from SpringRoll/SpringRoll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaptionPlayer.js
82 lines (74 loc) · 1.96 KB
/
CaptionPlayer.js
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
75
76
77
78
79
80
81
82
import { Debugger } from '../../debug/Debugger';
import { CaptionFactory } from './CaptionFactory';
/**
* @typedef {import('./renderers/IRenderer.js').IRender} IRender
*/
/**
* CaptionPlayer is used to start, stop and update captions.
* It applies the content of an active caption to a given CaptionRenderer.
*
* @export
* @class CaptionPlayer
/*
*
* @export
* @class CaptionPlayer
*/
export class CaptionPlayer {
/**
* Creates an instance of CaptionPlayer.
* @param {*} captions - Captions map.
* @param {IRender} renderer CaptionRenderer that content is applied to.
* @memberof CaptionPlayer
*/
constructor(captions, renderer) {
this.captions = CaptionFactory.createCaptionMap(captions);
this.renderer = renderer;
this.activeCaption = null;
}
/**
* Updates any currently playing caption.
* This ~should~ be called every frame.
*
* @param {Number} deltaTime Time passed in seconds since last update call.
* @memberof CaptionPlayer
*/
update(deltaTime) {
if (this.activeCaption) {
this.activeCaption.update(deltaTime);
if (this.activeCaption.isFinished()) {
this.stop();
}
}
}
/**
* Starts playing a caption.
*
* @param {String} name Name of caption.
* @param {number} [time=0] Atart time in milliseconds.
* @param {object} [args = {}] Arguments that will get passed to the renderer
* @memberof CaptionPlayer
*/
start(name, time = 0, args = {}) {
this.stop();
this.activeCaption = this.captions[name];
if (this.activeCaption) {
this.renderer.start(args);
this.activeCaption.start(time, this.renderer);
return;
}
Debugger.log('warn', `[CaptionPlayer.Start()] caption ${name} not found`);
}
/**
* Stops any caption currently playing.
* @memberof CaptionPlayer
*/
stop() {
if (this.activeCaption) {
if (this.renderer.stop) {
this.renderer.stop();
}
}
this.activeCaption = null;
}
}