Skip to content

Commit a978a99

Browse files
committed
Add Buffered State
1 parent 8a60c60 commit a978a99

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

src/VidaaVideo/VidaaVideo.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,48 @@ function VidaaVideo(options) {
3333
};
3434
videoElement.ontimeupdate = function() {
3535
onPropChanged('time');
36+
onPropChanged('buffered');
3637
};
3738
videoElement.ondurationchange = function() {
3839
onPropChanged('duration');
3940
};
4041
videoElement.onwaiting = function() {
4142
onPropChanged('buffering');
43+
onPropChanged('buffered');
4244
};
4345
videoElement.onseeking = function() {
4446
onPropChanged('time');
4547
onPropChanged('buffering');
48+
onPropChanged('buffered');
4649
};
4750
videoElement.onseeked = function() {
4851
onPropChanged('time');
4952
onPropChanged('buffering');
53+
onPropChanged('buffered');
5054
};
5155
videoElement.onstalled = function() {
5256
onPropChanged('buffering');
57+
onPropChanged('buffered');
5358
};
5459
videoElement.onplaying = function() {
5560
onPropChanged('time');
5661
onPropChanged('buffering');
62+
onPropChanged('buffered');
5763
};
5864
videoElement.oncanplay = function() {
5965
onPropChanged('buffering');
66+
onPropChanged('buffered');
6067
};
6168
videoElement.canplaythrough = function() {
6269
onPropChanged('buffering');
70+
onPropChanged('buffered');
6371
};
6472
videoElement.onloadedmetadata = function() {
6573
onPropChanged('loaded');
6674
};
6775
videoElement.onloadeddata = function() {
6876
onPropChanged('buffering');
77+
onPropChanged('buffered');
6978
};
7079
videoElement.onvolumechange = function() {
7180
onPropChanged('volume');
@@ -105,6 +114,7 @@ function VidaaVideo(options) {
105114
time: false,
106115
duration: false,
107116
buffering: false,
117+
buffered: false,
108118
subtitlesTracks: false,
109119
selectedSubtitlesTrackId: false,
110120
audioTracks: false,
@@ -154,6 +164,20 @@ function VidaaVideo(options) {
154164

155165
return videoElement.readyState < videoElement.HAVE_FUTURE_DATA;
156166
}
167+
case 'buffered': {
168+
if (stream === null) {
169+
return null;
170+
}
171+
172+
var time = videoElement.currentTime !== null && isFinite(videoElement.currentTime) ? videoElement.currentTime : 0;
173+
for (var i = 0; i < videoElement.buffered.length; i++) {
174+
if (videoElement.buffered.start(i) <= time && time <= videoElement.buffered.end(i)) {
175+
return Math.floor(videoElement.buffered.end(i) * 1000);
176+
}
177+
}
178+
179+
return Math.floor(time * 1000);
180+
}
157181
case 'subtitlesTracks': {
158182
if (stream === null) {
159183
return [];
@@ -411,6 +435,7 @@ function VidaaVideo(options) {
411435
onPropChanged('time');
412436
onPropChanged('duration');
413437
onPropChanged('buffering');
438+
onPropChanged('buffered');
414439
if (videoElement.textTracks) {
415440
videoElement.textTracks.onaddtrack = function() {
416441
setTimeout(function() {
@@ -469,6 +494,7 @@ function VidaaVideo(options) {
469494
onPropChanged('time');
470495
onPropChanged('duration');
471496
onPropChanged('buffering');
497+
onPropChanged('buffered');
472498
onPropChanged('subtitlesTracks');
473499
onPropChanged('selectedSubtitlesTrackId');
474500
onPropChanged('audioTracks');
@@ -550,7 +576,7 @@ VidaaVideo.canPlayStream = function(stream) {
550576
VidaaVideo.manifest = {
551577
name: 'VidaaVideo',
552578
external: false,
553-
props: ['stream', 'loaded', 'paused', 'time', 'duration', 'buffering', 'audioTracks', 'selectedAudioTrackId', 'subtitlesTracks', 'selectedSubtitlesTrackId', 'volume', 'muted', 'playbackSpeed'],
579+
props: ['stream', 'loaded', 'paused', 'time', 'duration', 'buffering', 'buffered', 'audioTracks', 'selectedAudioTrackId', 'subtitlesTracks', 'selectedSubtitlesTrackId', 'volume', 'muted', 'playbackSpeed'],
554580
commands: ['load', 'unload', 'destroy'],
555581
events: ['propValue', 'propChanged', 'ended', 'error', 'subtitlesTrackLoaded', 'audioTrackLoaded']
556582
};

0 commit comments

Comments
 (0)