Add custom headers to videojs player source

前端 未结 2 1630
一向
一向 2021-01-26 12:15

I have a backend api (using express) that handles a streaming video m3u8 file.

http://localhost:3000/api/stream.m3u8

This endpoint will only wo

相关标签:
2条回答
  • 2021-01-26 12:42

    Videojs initialization is happening on client side so you need to pass token before this initialization. I suggest you to initialize Videojs using ajax and send token using Ajax. I hope it helps.

    0 讨论(0)
  • 2021-01-26 12:45

    The solution I've gone with was to intercept browser XHR requests(using the xhook npm package) and set headers programmatically. Bellow is a simple example:

    ...
    
    this.player.src({
      src: SOME_VIDEO_URL,
    });
    
    ...
    
    /**
     * add manifest authorization header
     */
    window.xhook.before((request, callback) => {
      // only set header request for the videojs src url (don't touch other xhr requests)
      if (request.url === SOME_VIDEO_URL) {
        request.xhr.setRequestHeader('Authorization', manifestAuthorization);
      }
      callback();
    });
    
    0 讨论(0)
提交回复
热议问题