Skip to content

Commit

Permalink
fix: 🐛 (xgplayer-hls) 修复重播时,视频播放到结尾一直loading的问题(重播时末尾buffer已下载)
Browse files Browse the repository at this point in the history
  • Loading branch information
gemxx committed May 28, 2024
1 parent 5b186db commit e8f3115
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/xgplayer-hls/src/hls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,10 @@ export class Hls extends EventEmitter {
*/
_loadSegment = async () => {
if (this._segmentProcessing || !this.media) return
const nextSeg = this._playlist.nextSegment
const { nextSegment } = this._playlist
const { config } = this

if (!nextSeg) return
if (!nextSegment) return

if (!this.isLive) {
let bInfo = this.bufferInfo()
Expand All @@ -492,9 +492,9 @@ export class Hls extends EventEmitter {

// reset segment pointer by buffer end
if (!this._urlSwitching &&
this._prevSegSn !== nextSeg.sn - 1 &&
this._prevSegSn !== nextSegment.sn - 1 &&
bInfo.end &&
Math.abs(nextSeg.start - bInfo.end) > 1) {
Math.abs(nextSegment.start - bInfo.end) > 1) {
this._playlist.setNextSegmentByIndex(this._playlist.findSegmentIndexByTime(bInfo.end + 0.1))
}
}
Expand Down Expand Up @@ -916,7 +916,11 @@ export class Hls extends EventEmitter {
const { media } = this
const { nextSegment, lastSegment } = this._playlist
const eosAllowed =
!nextSegment &&
(!nextSegment ||
(lastSegment &&
// Use the mid-time of lastSegment to determine
// whether the Media Buffer has been buffered to the end
Buffer.isBuffered(media, lastSegment.start + lastSegment.duration / 2))) &&
media.readyState &&
media.duration > 0 &&
this._bufferService?.msIsOpened &&
Expand Down
21 changes: 21 additions & 0 deletions packages/xgplayer-streaming-shared/src/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,25 @@ export class Buffer {
length: Buffer.totalLength && Buffer.totalLength(buffers)
}
}

/**
*
* @param {HTMLMediaElement} media
* @param {number} pos
* @returns {Boolean}
*/
static isBuffered (media, pos) {
if (media) {
const buffered = Buffer.get(media)

if (buffered?.length) {
for (let i = 0; i < buffered.length; i++) {
if (pos >= buffered.start(i) && pos <= buffered.end(i)) {
return true
}
}
}
}
return false
}
}

0 comments on commit e8f3115

Please sign in to comment.