Skip to content

Commit 3974fbd

Browse files
authored
Merge pull request #57 from Stremio/feat/subtitles-opacity
Subtitles opacity
2 parents 55c83fa + cb56794 commit 3974fbd

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

src/TizenVideo/TizenVideo.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function TizenVideo(options) {
2424
var textColor = 'rgb(255, 255, 255)';
2525
var backgroundColor = 'rgba(0, 0, 0, 0)';
2626
var outlineColor = 'rgb(34, 34, 34)';
27+
var subtitlesOpacity = 1;
2728

2829
var objElement = document.createElement('object');
2930
objElement.type = 'application/avplayer';
@@ -59,6 +60,8 @@ function TizenVideo(options) {
5960
}
6061

6162
subtitlesElement.style.bottom = offset + '%';
63+
subtitlesElement.style.opacity = subtitlesOpacity;
64+
6265
var cueNode = document.createElement('span');
6366
cueNode.innerHTML = text;
6467
cueNode.style.display = 'inline-block';
@@ -138,6 +141,7 @@ function TizenVideo(options) {
138141
subtitlesTextColor: false,
139142
subtitlesBackgroundColor: false,
140143
subtitlesOutlineColor: false,
144+
subtitlesOpacity: false,
141145
audioTracks: false,
142146
selectedAudioTrackId: false,
143147
playbackSpeed: false
@@ -272,6 +276,13 @@ function TizenVideo(options) {
272276

273277
return outlineColor;
274278
}
279+
case 'subtitlesOpacity': {
280+
if (destroyed) {
281+
return null;
282+
}
283+
284+
return subtitlesOpacity;
285+
}
275286
case 'audioTracks': {
276287
if (stream === null) {
277288
return [];
@@ -506,6 +517,22 @@ function TizenVideo(options) {
506517

507518
break;
508519
}
520+
case 'subtitlesOpacity': {
521+
if (typeof propValue === 'number') {
522+
try {
523+
subtitlesOpacity = Math.min(Math.max(propValue / 100, 0), 1);
524+
} catch (error) {
525+
// eslint-disable-next-line no-console
526+
console.error('Tizen player with HTML Subtitles', error);
527+
}
528+
529+
refreshSubtitle();
530+
531+
onPropChanged('subtitlesOpacity');
532+
}
533+
534+
break;
535+
}
509536
case 'selectedAudioTrackId': {
510537
if (stream !== null) {
511538

@@ -608,6 +635,7 @@ function TizenVideo(options) {
608635
onPropChanged('subtitlesTextColor');
609636
onPropChanged('subtitlesBackgroundColor');
610637
onPropChanged('subtitlesOutlineColor');
638+
onPropChanged('subtitlesOpacity');
611639
onPropChanged('playbackSpeed');
612640
events.removeAllListeners();
613641
containerElement.removeChild(objElement);
@@ -657,7 +685,7 @@ TizenVideo.canPlayStream = function() {
657685
TizenVideo.manifest = {
658686
name: 'TizenVideo',
659687
external: false,
660-
props: ['stream', 'paused', 'time', 'duration', 'buffering', 'audioTracks', 'selectedAudioTrackId', 'subtitlesTracks', 'selectedSubtitlesTrackId', 'subtitlesOffset', 'subtitlesSize', 'subtitlesTextColor', 'subtitlesBackgroundColor', 'subtitlesOutlineColor', 'playbackSpeed'],
688+
props: ['stream', 'paused', 'time', 'duration', 'buffering', 'audioTracks', 'selectedAudioTrackId', 'subtitlesTracks', 'selectedSubtitlesTrackId', 'subtitlesOffset', 'subtitlesSize', 'subtitlesTextColor', 'subtitlesBackgroundColor', 'subtitlesOutlineColor', 'subtitlesOpacity', 'playbackSpeed'],
661689
commands: ['load', 'unload', 'destroy'],
662690
events: ['propValue', 'propChanged', 'ended', 'error', 'subtitlesTrackLoaded', 'audioTrackLoaded']
663691
};

src/WebOsVideo/WebOsVideo.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ function WebOsVideo(options) {
430430
var stream = null;
431431
var startTime = null;
432432
var subtitlesOffset = 0;
433+
var subtitlesOpacity = 255;
433434
var observedProps = {
434435
stream: false,
435436
paused: false,
@@ -443,6 +444,7 @@ function WebOsVideo(options) {
443444
subtitlesSize: false,
444445
subtitlesTextColor: false,
445446
subtitlesBackgroundColor: false,
447+
subtitlesOpacity: false,
446448
audioTracks: false,
447449
selectedAudioTrackId: false,
448450
volume: false,
@@ -539,6 +541,13 @@ function WebOsVideo(options) {
539541

540542
return lastSubBgColor || 'rgba(255, 255, 255, 0)';
541543
}
544+
case 'subtitlesOpacity': {
545+
if (destroyed) {
546+
return null;
547+
}
548+
549+
return subtitlesOpacity || 255;
550+
}
542551
case 'audioTracks': {
543552
return audioTracks;
544553
}
@@ -821,6 +830,22 @@ function WebOsVideo(options) {
821830

822831
break;
823832
}
833+
case 'subtitlesOpacity': {
834+
if (typeof propValue === 'number') {
835+
luna({
836+
method: 'setSubtitleBackgroundOpacity',
837+
parameters: {
838+
'mediaId': knownMediaId,
839+
'bgOpacity': Math.min(Math.max(propValue / 0.4, 0), 255),
840+
}
841+
});
842+
843+
subtitlesOpacity = propValue;
844+
onPropChanged('subtitlesOpacity');
845+
}
846+
847+
break;
848+
}
824849
case 'selectedAudioTrackId': {
825850
// console.log('WebOS', 'change audio track for id: ', knownMediaId, ' index:', propValue);
826851

@@ -1014,6 +1039,7 @@ function WebOsVideo(options) {
10141039
onPropChanged('subtitlesSize');
10151040
onPropChanged('subtitlesTextColor');
10161041
onPropChanged('subtitlesBackgroundColor');
1042+
onPropChanged('subtitlesOpacity');
10171043
onPropChanged('volume');
10181044
onPropChanged('muted');
10191045
onPropChanged('playbackSpeed');
@@ -1084,7 +1110,7 @@ WebOsVideo.canPlayStream = function() { // function(stream)
10841110
WebOsVideo.manifest = {
10851111
name: 'WebOsVideo',
10861112
external: false,
1087-
props: ['stream', 'paused', 'time', 'duration', 'buffering', 'buffered', 'audioTracks', 'selectedAudioTrackId', 'subtitlesTracks', 'selectedSubtitlesTrackId', 'subtitlesOffset', 'subtitlesSize', 'subtitlesTextColor', 'subtitlesBackgroundColor', 'volume', 'muted', 'playbackSpeed'],
1113+
props: ['stream', 'paused', 'time', 'duration', 'buffering', 'buffered', 'audioTracks', 'selectedAudioTrackId', 'subtitlesTracks', 'selectedSubtitlesTrackId', 'subtitlesOffset', 'subtitlesSize', 'subtitlesTextColor', 'subtitlesBackgroundColor', 'subtitlesOpacity', 'volume', 'muted', 'playbackSpeed'],
10881114
commands: ['load', 'unload', 'destroy'],
10891115
events: ['propValue', 'propChanged', 'ended', 'error', 'subtitlesTrackLoaded', 'audioTrackLoaded']
10901116
};

src/withHTMLSubtitles/withHTMLSubtitles.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function withHTMLSubtitles(Video) {
5353
var textColor = 'rgb(255, 255, 255)';
5454
var backgroundColor = 'rgba(0, 0, 0, 0)';
5555
var outlineColor = 'rgb(34, 34, 34)';
56+
var opacity = 1;
5657
var observedProps = {
5758
extraSubtitlesTracks: false,
5859
selectedExtraSubtitlesTrackId: false,
@@ -61,7 +62,8 @@ function withHTMLSubtitles(Video) {
6162
extraSubtitlesOffset: false,
6263
extraSubtitlesTextColor: false,
6364
extraSubtitlesBackgroundColor: false,
64-
extraSubtitlesOutlineColor: false
65+
extraSubtitlesOutlineColor: false,
66+
extraSubtitlesOpacity: false
6567
};
6668

6769
function renderSubtitles() {
@@ -74,6 +76,7 @@ function withHTMLSubtitles(Video) {
7476
}
7577

7678
subtitlesElement.style.bottom = offset + '%';
79+
subtitlesElement.style.opacity = opacity;
7780
subtitlesRenderer.render(cuesByTime, videoState.time - delay).forEach(function(cueNode) {
7881
cueNode.style.display = 'inline-block';
7982
cueNode.style.padding = '0.2em';
@@ -178,6 +181,13 @@ function withHTMLSubtitles(Video) {
178181

179182
return outlineColor;
180183
}
184+
case 'extraSubtitlesOpacity': {
185+
if (destroyed) {
186+
return null;
187+
}
188+
189+
return opacity;
190+
}
181191
default: {
182192
return videoPropValue;
183193
}
@@ -192,7 +202,8 @@ function withHTMLSubtitles(Video) {
192202
case 'extraSubtitlesOffset':
193203
case 'extraSubtitlesTextColor':
194204
case 'extraSubtitlesBackgroundColor':
195-
case 'extraSubtitlesOutlineColor': {
205+
case 'extraSubtitlesOutlineColor':
206+
case 'extraSubtitlesOpacity': {
196207
events.emit('propValue', propName, getProp(propName, null));
197208
observedProps[propName] = true;
198209
return true;
@@ -326,6 +337,21 @@ function withHTMLSubtitles(Video) {
326337

327338
return true;
328339
}
340+
case 'extraSubtitlesOpacity': {
341+
if (typeof propValue === 'number') {
342+
try {
343+
opacity = Math.min(Math.max(propValue / 100, 0), 1);
344+
} catch (error) {
345+
// eslint-disable-next-line no-console
346+
console.error('withHTMLSubtitles', error);
347+
}
348+
349+
renderSubtitles();
350+
onPropChanged('extraSubtitlesOpacity');
351+
}
352+
353+
return true;
354+
}
329355
default: {
330356
return false;
331357
}
@@ -387,6 +413,7 @@ function withHTMLSubtitles(Video) {
387413
onPropChanged('extraSubtitlesTextColor');
388414
onPropChanged('extraSubtitlesBackgroundColor');
389415
onPropChanged('extraSubtitlesOutlineColor');
416+
onPropChanged('extraSubtitlesOpacity');
390417
video.dispatch({ type: 'command', commandName: 'destroy' });
391418
events.removeAllListeners();
392419
containerElement.removeChild(subtitlesElement);
@@ -448,7 +475,7 @@ function withHTMLSubtitles(Video) {
448475
VideoWithHTMLSubtitles.manifest = {
449476
name: Video.manifest.name + 'WithHTMLSubtitles',
450477
external: Video.manifest.external,
451-
props: Video.manifest.props.concat(['extraSubtitlesTracks', 'selectedExtraSubtitlesTrackId', 'extraSubtitlesDelay', 'extraSubtitlesSize', 'extraSubtitlesOffset', 'extraSubtitlesTextColor', 'extraSubtitlesBackgroundColor', 'extraSubtitlesOutlineColor'])
478+
props: Video.manifest.props.concat(['extraSubtitlesTracks', 'selectedExtraSubtitlesTrackId', 'extraSubtitlesDelay', 'extraSubtitlesSize', 'extraSubtitlesOffset', 'extraSubtitlesTextColor', 'extraSubtitlesBackgroundColor', 'extraSubtitlesOutlineColor', 'extraSubtitlesOpacity'])
452479
.filter(function(value, index, array) { return array.indexOf(value) === index; }),
453480
commands: Video.manifest.commands.concat(['load', 'unload', 'destroy', 'addExtraSubtitlesTracks'])
454481
.filter(function(value, index, array) { return array.indexOf(value) === index; }),

0 commit comments

Comments
 (0)