Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyiya committed Aug 25, 2022
1 parent 6a87d29 commit 3d3a849
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions packages/hls/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const hlsPlugin = ({
matcher = defaultMatcher
}: hlsPluginOptions = {}): PlayerPlugin => {
let isInitial = false
let hlsInstance: Hls | null = null
let hlsInstance: Hls

const getHls = (options?: Partial<HlsConfig>): Hls => {
if (hlsInstance) {
Expand All @@ -33,12 +33,12 @@ const hlsPlugin = ({

return {
name: PLUGIN_NAME,
load: ({ on, emit, offAny }, video, source) => {
load: ({ on, emit }, video, source) => {
if (!matcher(video, source)) return false

hlsInstance = getHls({ autoStartLoad: false, ...hlsConfig })

if (!hlsInstance || !Hls.isSupported()) {
if (!Hls.isSupported()) {
emit('pluginerror', {
payload: {
type: 'hlsNotSupported',
Expand All @@ -51,31 +51,39 @@ const hlsPlugin = ({
if (!isInitial) {
emit('loadedplugin', { name: PLUGIN_NAME })
isInitial = true
offAny('canplay') //onReady is handled by hls.js
}

hlsInstance?.attachMedia(video)
hlsInstance?.loadSource(source.src)
hlsInstance?.startLoad()
hlsInstance.attachMedia(video)
hlsInstance.loadSource(source.src)
hlsInstance.startLoad()

//TODO: remove video onReady Listener
// onReady is handled by hls.js
// hlsInstance.on(
// Hls.Events.MANIFEST_PARSED,
// (event: Events.MANIFEST_PARSED, data: ManifestParsedData) => {
// emit('canplay', { type: event, payload: data })
// }
// )

Object.values(Hls.Events).forEach((e) => {
hlsInstance?.on(e as any, (event: string, data: ErrorData) => {
if (
event === Hls.Events.ERROR &&
data.details == 'manifestLoadError' /*ErrorDetails.MANIFEST_LOAD_ERROR*/
) {
hlsInstance.on(e as any, (event: string, data: ErrorData) => {
if (event === Hls.Events.ERROR && data.details == Hls.ErrorDetails.MANIFEST_LOAD_ERROR) {
emit('pluginerror', { message: data.type, ...data })
}
emit(event, data)
})
})

on('destroy', () => {
hlsInstance?.destroy()
hlsInstance = null
hlsInstance.destroy()
hlsInstance = null as any
})

return true
},
apply: () => {
// restore video onready listener
}
}
}
Expand Down

0 comments on commit 3d3a849

Please sign in to comment.