Skip to content

Commit

Permalink
feat: 优化字幕偏移逻辑,增加对模板的支持,修复字幕切换事件参数
Browse files Browse the repository at this point in the history
  • Loading branch information
zhw2590582 committed Sep 14, 2024
1 parent 36b2c80 commit 9b87f96
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
20 changes: 8 additions & 12 deletions docs/uncompiled/artplayer/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/uncompiled/artplayer/index.js.map

Large diffs are not rendered by default.

22 changes: 8 additions & 14 deletions packages/artplayer/src/player/subtitleOffsetMix.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import { def } from '../utils';
import { def, clamp } from '../utils';

export default function subtitleOffsetMix(art) {
const { clamp } = art.constructor.utils;
const { notice, i18n } = art;

let subtitleOffset = 0;

art.on('subtitleTrackLoad', () => {
subtitleOffset = 0;
art.emit('subtitleOffset', 0);
});
const { notice, i18n, template } = art;

def(art, 'subtitleOffset', {
get() {
return subtitleOffset;
return template.$track?.offset || 0;
},
set(value) {
const { cues } = art.subtitle;
subtitleOffset = clamp(value, -5, 5);
if (!template.$track || cues.length === 0) return;
const offset = clamp(value, -5, 5);
template.$track.offset = offset;
for (let index = 0; index < cues.length; index++) {
const cue = cues[index];
cue.originalStartTime = cue.originalStartTime ?? cue.startTime;
cue.originalEndTime = cue.originalEndTime ?? cue.endTime;
cue.startTime = clamp(cue.originalStartTime + subtitleOffset, 0, art.duration);
cue.endTime = clamp(cue.originalEndTime + subtitleOffset, 0, art.duration);
cue.startTime = clamp(cue.originalStartTime + offset, 0, art.duration);
cue.endTime = clamp(cue.originalEndTime + offset, 0, art.duration);
}
art.subtitle.update();
notice.show = `${i18n.get('Subtitle Offset')}: ${value}s`;
Expand Down
2 changes: 1 addition & 1 deletion packages/artplayer/src/subtitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class Subtitle extends Component {
if (this.url === subUrl) return subUrl;
URL.revokeObjectURL(this.url);
this.createTrack('metadata', subUrl);
this.art.emit('subtitleSwitch', subUrl);
this.art.emit('subtitleSwitch', subUrl, subtitleOption.url);
return subUrl;
})
.catch((err) => {
Expand Down

0 comments on commit 9b87f96

Please sign in to comment.