【问题标题】:display custom error message in a live Player在实时播放器中显示自定义错误消息
【发布时间】:2021-07-09 20:22:22
【问题描述】:

我有一个如下所示的 react 代码,它在页面加载时呈现播放器。仅当条件为真时,代码才会进入 if 块。

const player = ()=> {
    if(condition) {
        return (
            <ReactJWPlayer
                playlist={[props.playlist]}
            />
        )
    }
}

问题说明:对于错误代码232011,我看到以下错误消息:

[![在此处输入图片描述][1]][1]

This video file cannot be played
(Error Code: 232011)

我想知道我需要在上面的反应代码中进行哪些更改,以便我可以将上面的错误消息替换为播放器中的以下错误消息。

Video will be available soon

【问题讨论】:

  • 问题中缺少关键细节。例如,我们需要知道ReactJWPlayer 组件的结构,以便弄清楚要进行的更改。
  • 你能在codeandbox中在线重现问题吗?
  • @DimaParzhitsky 我们做到了npm install react-jw-player.js npmjs.com/package/react-jw-player
  • 我想我必须使用 jwplayer().on('error') and jwplayer().on('setupError') 方法,在我们的开发人员文档中找到。 developer.jwplayer.com/jwplayer/docs/… 但我不确定如何集成到我正在使用的当前代码结构中。

标签: javascript reactjs jwplayer


【解决方案1】:

你必须使用intl.{lang}.errors 对象。该对象本地化播放器中显示的错误消息。

为了配置 intl.{lang}.errors,您必须使用 react-jw-player 公开的 customProps 选项直接应用于 JW Player 实例。

您可以只使用en,或者根据您的用例添加额外的语言支持。

import { useRef } from "react";
import ReactJWPlayer from "react-jw-player";
import ReactDOM from "react-dom";

export default function App() {
  const jwPlayerRef = useRef();

  const myErrorHandler = (err) => {
    console.log(err);
   // Find the Node where error message is shown
    const errorNode = ReactDOM.findDOMNode(
      jwPlayerRef.current
    ).getElementsByClassName("jw-error-text");
    // If the error node exists, replace both message and code
    if (errorNode && errorNode.length)
      errorNode[0].innerText = "Custom Error Message";
  };

  return (
    <div className="App">
      <div
        className="jw-video-container"
        data-mediaid="TAITbudl"
        style={{ height: "100%", width: "100%" }}
      >
        <ReactJWPlayer
          ref={jwPlayerRef}
          playerId="TAITbudl"
          playerScript="https://content.jwplatform.com/libraries/j9BLvpMc.js"
          playlist="https://cdn.jwplayer.com/v2/media/123"
          onError={myErrorHandler}
          onSetupError={myErrorHandler}
          customProps={{ // <= Official way to override the error message
            intl: {
              en: {
                errors: {
                  badConnection:
                    "This video cannot be played because of a problem with your internet connection.",
                  cantLoadPlayer: "Sorry, the video player failed to load.",
                  cantPlayInBrowser:
                    "The video cannot be played in this browser.",
                  cantPlayVideo: "This is my custom error Message",
                  errorCode: "Code - ",
                  liveStreamDown:
                    "The live stream is either down or has ended.",
                  protectedContent:
                    "There was a problem providing access to protected content.",
                  technicalError:
                    "This video cannot be played because of a technical error."
                }
              }
            }
          }}
        />
      </div>
    </div>
  );
}

intl 对象允许您添加新的语言翻译,[...] - Docs

请注意,getElementsByClassName("jw-error-text") 是一个 hack,如果 JW Player 决定更改类名或对其进行混淆,这个 hack 将会中断。

【讨论】:

  • 感谢您让我了解国际对象。我已经阅读了它的文档,在添加新的语言翻译时它似乎非常有用。我打开沙盒,我可以在文本消息下方的播放器中看到error code。有什么办法可以摆脱(Code-:102400) 行吗?
  • 我找不到任何由 JW Player 公开的允许隐藏错误代码的 API。如果您查看 JW 播放器脚本文件,您会发现错误代码来自内部 API,并且无法自定义。也就是说,我已经更新了答案和代码沙箱以消除错误代码。
  • 好吧,有道理。谢谢你让我知道。我想知道ref={jwPlayerRef}&lt;ReactJWPlayer 中有什么用?
  • 用于直接操作DOM。reactjs.org/docs/refs-and-the-dom.html
  • 嗨,我正在关注我的问题。 cantPlayVideo 属性涵盖的错误代码列表是什么?我在documentation中找不到任何信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-29
  • 2021-06-06
  • 1970-01-01
相关资源
最近更新 更多