Browse Source

feat(sub): init subtitles options

yuusora 3 years ago
parent
commit
d18ea10e1f
1 changed files with 41 additions and 0 deletions
  1. 41 0
      src/js/player.js

+ 41 - 0
src/js/player.js

@@ -10,6 +10,7 @@ import Events from './events';
 import FullScreen from './fullscreen';
 import User from './user';
 import Subtitle from './subtitle';
+import Subtitles from './subtitles';
 import Bar from './bar';
 import Timer from './timer';
 import Bezel from './bezel';
@@ -60,6 +61,41 @@ class DPlayer {
             this.container.classList.add('dplayer-arrow');
         }
 
+        // multi subtitles defaultSubtitle add index, off option
+        if (Array.isArray(this.options.subtitle.url)) {
+            const offSubtitle = {
+                subtitle: '',
+                lang: 'Off',
+            };
+            this.options.subtitle.url.push(offSubtitle);
+            if (this.options.subtitle.defaultSubtitle) {
+                if (typeof this.options.subtitle.defaultSubtitle === 'string') {
+                    // defaultSubtitle is string, match in lang then name.
+                    this.options.subtitle.index = this.options.subtitle.url.findIndex((sub) =>
+                        /* if (sub.lang === this.options.subtitle.defaultSubtitle) {
+                            return true;
+                        } else if (sub.name === this.options.subtitle.defaultSubtitle) {
+                            return true;
+                        } else {
+                            return false;
+                        } */
+                        sub.lang === this.options.subtitle.defaultSubtitle ? true : sub.name === this.options.subtitle.defaultSubtitle ? true : false
+                    );
+                } else if (typeof this.options.subtitle.defaultSubtitle === 'number') {
+                    // defaultSubtitle is int, directly use for index
+                    this.options.subtitle.index = this.options.subtitle.defaultSubtitle;
+                }
+            }
+            // defaultSubtitle not match or not exist or index bound(when defaultSubtitle is int), try browser language.
+            if (this.options.subtitle.index === -1 || !this.options.subtitle.index || this.options.subtitle.index > this.options.subtitle.url.length - 1) {
+                this.options.subtitle.index = this.options.subtitle.url.findIndex((sub) => sub.lang === navigator.language);
+            }
+            // browser language not match, dedfault off title
+            if (this.options.subtitle.index === -1) {
+                this.options.subtitle.index = this.options.subtitle.url.length - 1;
+            }
+        }
+
         this.template = new Template({
             container: this.container,
             options: this.options,
@@ -519,7 +555,12 @@ class DPlayer {
         this.volume(this.user.get('volume'), true, true);
 
         if (this.options.subtitle) {
+            // init old single subtitle function(sub show and style)
             this.subtitle = new Subtitle(this.template.subtitle, this.video, this.options.subtitle, this.events);
+            // init multi subtitles function(sub update)
+            if (Array.isArray(this.options.subtitle.url)) {
+                this.subtitles = new Subtitles(this);
+            }
             if (!this.user.get('subtitle')) {
                 this.subtitle.hide();
             }