Skip to content

Commit

Permalink
fix(xgplayer): 分段 播放增加loop处理,并修复offsetCurrentTime获取异常问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hongqx committed Aug 5, 2024
1 parent 1719439 commit 7b06717
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 51 deletions.
70 changes: 61 additions & 9 deletions packages/xgplayer/src/plugins/time/timesegments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
import { BasePlugin, Events, Util } from '../../plugin'
export function getIndexByTime (time, segments) {
const _len = segments.length
let _index = -1
if (_len < 1) {
return _index
}
if (time <= segments[0].end || _len < 2) {
_index = 0
} else if (time > segments[_len - 1].end) {
_index = _len - 1
} else {
for (let i = 1; i < _len; i++) {
if (time > segments[i - 1].end && time <= segments[i].end){
_index = i
break
}
}
}
return _index
}

export function getOffsetCurrentTime (currentTime, segments, index = -1) {
let _index = -1
if (index >= 0 && index < segments.length) {
_index = index
} else {
_index = getIndexByTime(currentTime, segments)
}
if (_index < 0) {
return -1
}
const _len = segments.length
const { start, end, cTime, offset } = segments[_index]
if (currentTime < start) {
return cTime
} else if (currentTime >= start && currentTime <= end) {
return currentTime - offset
} else if (currentTime > end && _index >= _len - 1) {
return end - offset
}
return -1
}

/**
* 进行事件分段控制
*/
Expand Down Expand Up @@ -97,8 +140,8 @@ export default class TimeSegmentsControls extends BasePlugin {
if (!this._checkIfEnabled(timeSegments)) {
return
}
const index = Util.getIndexByTime(currentTime,timeSegments)
const time = Util.getOffsetCurrentTime(currentTime, timeSegments, index)
const index = getIndexByTime(currentTime,timeSegments)
const time = getOffsetCurrentTime(currentTime, timeSegments, index)
this.player.offsetCurrentTime = time
this.changeIndex(index, timeSegments)
}
Expand All @@ -108,7 +151,7 @@ export default class TimeSegmentsControls extends BasePlugin {
if (!this._checkIfEnabled(timeSegments)) {
return
}
const time = Util.getOffsetCurrentTime(0, timeSegments)
const time = getOffsetCurrentTime(0, timeSegments)
this.player.offsetCurrentTime = time
this.changeIndex(0, timeSegments)
if (this.curPos.start > 0){
Expand All @@ -123,12 +166,11 @@ export default class TimeSegmentsControls extends BasePlugin {
}
const _len = timeSegments.length
this.lastCurrentTime = currentTime
const index = Util.getIndexByTime(currentTime, timeSegments)
const index = getIndexByTime(currentTime, timeSegments)
if (index !== this.curIndex) {
this.changeIndex(index, timeSegments)
}
const curTime = Util.getOffsetCurrentTime(currentTime, timeSegments, index)

const curTime = getOffsetCurrentTime(currentTime, timeSegments, index)
this.player.offsetCurrentTime = curTime

// 根据分段信息进行放时间点跳转
Expand All @@ -139,6 +181,16 @@ export default class TimeSegmentsControls extends BasePlugin {
if (currentTime < start) {
this.player.currentTime = start
} else if (currentTime > end && index >= _len - 1) {
this.triggerCustomEnded()
}
}

triggerCustomEnded () {
const { loop } = this.playerConfig
if (loop) {
const time = this.convertVideoTime(0)
this.player.seek(time)
} else {
this.player.pause()
this.player.emit('ended')
}
Expand All @@ -154,7 +206,7 @@ export default class TimeSegmentsControls extends BasePlugin {
} else if (currentTime > timeSegments[timeSegments.length - 1].end) {
this.player.currentTime = timeSegments[timeSegments.length - 1].end
} else {
const _index = Util.getIndexByTime(currentTime, timeSegments)
const _index = getIndexByTime(currentTime, timeSegments)
if (_index >= 0) {
const _seekTime = this.getSeekTime(currentTime, this.lastCurrentTime, _index, timeSegments)
if (_seekTime >= 0 ) {
Expand All @@ -172,7 +224,8 @@ export default class TimeSegmentsControls extends BasePlugin {
}
const _len = timeSegments.length
if (offsetTime < timeSegments[0].duration) {
return offsetTime
const { start } = timeSegments[0]
return offsetTime + start
} else if (offsetTime >= timeSegments[_len - 1].duration) {
return timeSegments[_len - 1].end
} else {
Expand Down Expand Up @@ -201,7 +254,6 @@ export default class TimeSegmentsControls extends BasePlugin {
}

getSeekTime (currentTime, lastCurrentTime, index, timeSegments) {
console.log('>>>getSeekTime', currentTime, lastCurrentTime, index)
let _time = -1
const { start, end } = timeSegments[index]
if (currentTime < timeSegments[0].start) {
Expand Down
42 changes: 0 additions & 42 deletions packages/xgplayer/src/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -882,48 +882,6 @@ util.convertDeg = function (val) {
return val % 360
}

util.getIndexByTime = function (time, segments) {
const _len = segments.length
let _index = -1
if (_len < 1) {
return _index
}
if (time <= segments[0].end || _len < 2) {
_index = 0
} else if (time > segments[_len - 1].end) {
_index = _len - 1
} else {
for (let i = 1; i < _len; i++) {
if (time > segments[i - 1].end && time <= segments[i].end){
_index = i
break
}
}
}
return _index
}
util.getOffsetCurrentTime = function (currentTime, segments, index = -1) {
let _index = -1
if (index >= 0 && index < segments.length) {
_index = index
} else {
_index = util.getIndexByTime(currentTime, segments)
}
if (_index < 0) {
return -1
}
const _len = segments.length
const { start, end, cTime, offset } = segments[_index]
if (currentTime < start) {
return cTime
} else if (currentTime >= start && currentTime <= end) {
return currentTime - offset
} else if (currentTime > end && _index >= _len - 1) {
return end
}
return -1
}

/**
*
* @param {*} offsetTime
Expand Down

0 comments on commit 7b06717

Please sign in to comment.