Skip to content

Commit

Permalink
fix: Fix a hls bug when switching quality (zhihu#59)
Browse files Browse the repository at this point in the history
* fix: add plugin config. fix hls bug
  • Loading branch information
wangcheng authored and xiaoyuhen committed Apr 10, 2019
1 parent 0b5e874 commit 4a0e079
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
10 changes: 4 additions & 6 deletions packages/griffith-hls/src/Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ export default class Video extends Component {
hasLoadStarted = false

componentDidMount() {
const {autoStartLoad = false} = this.props
this.hls = new Hls({
autoStartLoad,
})
this.hls = new Hls({autoStartLoad: false})
this.hls.attachMedia(this.video)
const {sources} = this.props
const master = getMasterM3U8Blob(sources)
Expand Down Expand Up @@ -38,11 +35,12 @@ export default class Video extends Component {

componentWillUnmount() {
this.hls.destroy()
URL.revokeObjectURL(this.src)
}

render() {
// eslint-disable-next-line
const {onRef, currentQuality, src, paused, ...props} = this.props
// eslint-disable-next-line no-unused-vars
const {onRef, currentQuality, src, sources, paused, ...props} = this.props
return (
<video
ref={el => {
Expand Down
8 changes: 7 additions & 1 deletion packages/griffith-hls/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export {default} from './Video'
import VideoComponent from './Video'

export default {
pluginName: 'griffith-mp4',
VideoComponent,
willHandleSrcChange: true,
}
9 changes: 6 additions & 3 deletions packages/griffith-mp4/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable import/default */
import Player from './player'
export default Player
import VideoComponent from './player'

export default {
pluginName: 'griffith-mp4',
VideoComponent,
}
7 changes: 6 additions & 1 deletion packages/griffith/src/components/Video/NormalVideo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React from 'react'

// eslint-disable-next-line
export default ({onRef, paused, currentQuality, sources, ...props}) => (
const NormalVideo = ({onRef, paused, currentQuality, sources, ...props}) => (
<video {...props} ref={onRef} />
)

export default {
pluginName: 'native',
VideoComponent: NormalVideo,
}
25 changes: 15 additions & 10 deletions packages/griffith/src/components/Video/Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,25 @@ class Video extends Component {
}

componentDidUpdate(prevProps, prevState, snapshot) {
const {src, paused, volume} = this.props
const {src, paused, volume, format, useMSE} = this.props

/**
* 切换清晰度,如果是非 mse 视频(src 是 blob 类型)
* data 变化的时候会 remount,所以 componentDidUpdate 中 src 变化一定是清晰度变了
*/
if (prevProps.src && src !== prevProps.src) {
this.safeExecute(() => {
this.isMetadataLoaded = false
this.seek(snapshot.currentTime)
if (!snapshot.paused) {
this.play()
}
})
const {willHandleSrcChange} = selectVideo(format, useMSE)

// TODO 这一块逻辑需要 Video 自己处理
if (!willHandleSrcChange) {
this.safeExecute(() => {
this.isMetadataLoaded = false
this.seek(snapshot.currentTime)
if (!snapshot.paused) {
this.play()
}
})
}
}

if (paused !== prevProps.paused && paused !== this.root.paused) {
Expand Down Expand Up @@ -290,7 +295,7 @@ class Video extends Component {
currentQuality,
} = this.props

const Video = selectVideo(format, useMSE)
const {VideoComponent} = selectVideo(format, useMSE)

return (
<VideoWithMessage
Expand Down Expand Up @@ -323,7 +328,7 @@ class Video extends Component {
paused={paused}
sources={sources}
currentQuality={currentQuality}
Video={Video}
Video={VideoComponent}
/>
)
}
Expand Down

0 comments on commit 4a0e079

Please sign in to comment.