shiyiya / oplayer

:zap: Another HTM5 video player.
https://oplayer.vercel.app
MIT License
147 stars 19 forks source link

[Feature Request]Playlist plugin works together with hls plugin? #102

Closed leopku closed 11 months ago

leopku commented 11 months ago

OPlayer works well with m3u8 by hls plugin. But when I put hls plugin together with playlist plugin - which had sources with many m3u8 files, it throw MEDIA_ERR_SRC_NOT_SPPORTED.

Does any plan to add support for making playlist plugin work with hls plugin?

shiyiya commented 11 months ago

Looks like you can handle it yourself https://github.com/videojs/m3u8-parser

leopku commented 11 months ago

Appreciate for quick reply. I'll dive to make it work with playlist plugin.

shiyiya commented 11 months ago

pr welcome 😍

shiyiya commented 11 months ago

Hello, any progress? I'm going to implement it. Do you have a reference? https://raw.githubusercontent.com/fanmingming/live/main/tv/m3u/global.m3u

image
shiyiya commented 11 months ago

@leopku Can you provide your playlist for testing, thank you very much!

shiyiya commented 11 months ago
image
leopku commented 11 months ago

It worked great with static m3u8 list. Big thanks!

I'm now troubling with changing sources dynamically because the current of ref always null in useEffect. But it seems nothing about oplayer.

shiyiya commented 11 months ago

https://github.com/upvorg/upvorg.github.io/blob/80592b5003292296a9d1708d3ac5f9602f4b2f3a/packages/index/src/pages/player/index.tsx#L86

You can refer to this

leopku commented 11 months ago

https://github.com/upvorg/upvorg.github.io/blob/80592b5003292296a9d1708d3ac5f9602f4b2f3a/packages/index/src/pages/player/index.tsx#L86

You can refer to this

I reviewed the link above. It seems same codes with me. I create a sandbox to reproduce this problem. In the sandbox I put two elements all had a ref. The div element can get correct ref but the ReactPlayer element not. The link of sandbox was here: https://codesandbox.io/s/great-greider-77zygk?file=/src/App.tsx

shiyiya commented 11 months ago

oplayer also needs to wait for elm's ref to be created, which may be delayed.

leopku commented 11 months ago

How to implement it elegantly? thanks.

shiyiya commented 11 months ago

Your ajax should be slower than oplayer Why is this a problem?

leopku commented 11 months ago

I added a delayed print for player info, but it still got null.

    const timer = setTimeout(() => {
      console.log("player ref: ", playerRef, "div ref: ", divRef);
    }, 30000);
    return () => clearTimeout(timer);

I had updated the example sandbox.

leopku commented 11 months ago

奇怪的是,上午有段时间用 useCallback 是能获取 current 的。后来改来改去再也得不到 current 了。

shiyiya commented 11 months ago

tomorrow

leopku commented 11 months ago

I had the correct way to get ref by useCallback.

shiyiya commented 11 months ago

I had the correct way to get ref by useCallback.

how

leopku commented 11 months ago

Using useCallback to save ref. But there still had the null ref problem after first rendering. After first rendering, ref can be got correctly.

I had updated the sandbox.

shiyiya commented 11 months ago

你更新了还是null 感觉有点奇怪

leopku commented 11 months ago

后来找一个方法可以比较好的解决这个问题了。

function useHookWithRefCallback(onMount, onUnmount) {
  const ref = useRef<OPlayer | null>(null);
  const setRef = useCallback((node: OPlayer) => {
    if (ref.current) {
      onUnmount(ref.current);
    }

    ref.current = node;

    if (ref.current) {
      onMount(ref.current);
    }
  }, [onMount, onUnmount]);

  return setRef;
}

const { data } = useFetch({ url: 'xxx/api' });

const player = useHookWithRefCallback((el) => {
  // prepare sourceList here 
  el.context.playlist.changeSourceList(sourceList)
},
() => {})

实际使用中发现,由于动态变更了 sourceList, playlist 提供的方法还不够,建议加上 first() 方法和 play() 方法,在调用 changeSourceList() 方法后调用。

shiyiya commented 11 months ago

你这花里胡哨感觉也拿不到ref,可以用上上个版本不过严格模式下有问题。 first play 是?改变list后有方法可以调用播放第几个的好像

leopku commented 11 months ago

在 onMount 回调中拿到的直接就是 element,这是昨晚睡觉前已经调通的代码,目前运行良好。

改变list后有方法可以调用播放第几个的好像

就是因为 list 从初始化变成实装的 list 后要从 list 第一个开始播放。确认了一下,没有播放第几个的方法。已有的方法如下: apply, changeSource, changeSourceList, constructor, destroy, hideUI, isWaiting?, next, previous, render, renderList, showUI

leopku commented 11 months ago

Playlist 已经支持 m3u8 了,非常感谢,结帖了。

shiyiya commented 11 months ago

changeSource(idx: number) 这就是播放第几个

leopku commented 11 months ago

changeSource(idx: number) 这就是播放第几个

大意了,好使!

shiyiya commented 11 months ago

用1.0.10版吧 别hack了 严格模式修不来

leopku commented 11 months ago

用1.0.10版吧

OK

别 hack了 严格模式修不来

没明白。 useCallback 也不算黑科技,官方也提议 useCallback 跟 useRef 一起用的哈。