Send custom HTTP request header with HTML5 audio tag

后端 未结 3 596
-上瘾入骨i
-上瘾入骨i 2020-12-03 15:16

Is it possible to send custom HTTP request headers using the HTML5 audio tag? I\'m especially interested in sending a HTTP Range header, so the webserver will only stream pa

相关标签:
3条回答
  • 2020-12-03 15:42

    Not according to the spec, you can't. Audio tag is actually very inflexible.

    0 讨论(0)
  • 2020-12-03 15:53

    To answer the broad question... that will depend on what kind of request exactly.

    What you want[ed] to do is actually very possible. You can simply add a #t=start[,end] Media Fragment to any media url, and the browser will know it will have to only play from these timestamps:

    <!-- only from 23s to 27s -->
    <audio src="https://dl.dropboxusercontent.com/s/8c9m92u1euqnkaz/GershwinWhiteman-RhapsodyInBluePart1.mp3#t=23,27" controls></audio>

    And since I came from the future a question that did ask about sending authorization along with the media request, here is a link to some docs about the crossOrigin attribute, which will also make your HTTP request with "custom headers".

    0 讨论(0)
  • 2020-12-03 16:01

    If you're okay not supporting older browsers like IE, you can do this now with a service worker. Intercept the fetch request in the service worker and send it onwards with the custom header. Here is a basic example of intercepting a request to Google Drive and adding the Bearer token.

    self.addEventListener('fetch', function(event) {
      if(event.request.url === 'https://www.googleapis.com/drive/v3/files/fileID?alt=media') {
        event.respondWith(
            fetch(event.request.url, {
                method: "GET",
                headers: {
                    "Authorization": "Bearer myBearerToken",
                },
                redirect: "follow"
            })
        );    
      }
    });
    

    Edit: 2018-11-17 - Added how this won't work with older browsers.

    0 讨论(0)
提交回复
热议问题