javascript - 在实时播放器中显示自定义错误消息

标签 javascript reactjs jwplayer

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

const player = ()=> {
    if(condition) {
        return (
            <ReactJWPlayer
                playlist={[props.playlist]}
            />
        )
    }
}
问题陈述:对于错误代码232011 ,我看到以下错误消息:
[![在此处输入图像描述][1]][1]
This video file cannot be played
(Error Code: 232011)
我想知道我需要在上面的 react 代码中进行哪些更改,以便我可以将上面的错误消息替换为播放器中的以下错误消息。
Video will be available soon

最佳答案

您必须使用 intl.{lang}.errors 目的。此对象本地化播放器中显示的错误消息。
为了配置 intl.{lang}.errors ,您将不得不使用 customProps react-jw-player 公开的选项直接应用于 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>
  );
}

Edit React-JW-Player Custom Error Message

The intl object allows you to add new language translations, [...] - Docs


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

关于javascript - 在实时播放器中显示自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68322358/

相关文章:

javascript - JQuery 无法处理按钮的点击事件

javascript - 我如何使用 refs 来更改 ReactJS 中的样式类?

javascript - 如何在ReactJs中使用map渲染字符串数组?

reactjs - 为什么在 React 中,我的 axios API 调用具有包含 Bearer <token> 的授权 header ,但未获得授权并给出 401 错误

ios - ios中的jwplayer未捕获异常

javascript - 通过javascript将窗口对象从a.html传递到b.html

javascript - 如何在 JavaScript 中将数字格式化为不带小数点的货币?

javascript - 如何从JS中的数组中删除特定字符?

jwplayer - 设置后是否可以将字幕轨道添加到 jwplayer 实例?

android - 在 Android 上播放 hls/rtmp