Safari 浏览器无法加载 wav 文件类型的视频或音频

发布于 2025-01-09 03:49:05 字数 1675 浏览 0 评论 0原文

我一直在研究我们网站的一个错误,即通过 Safari 浏览器查看时,视频和 wav 类型音频文件无法加载/显示。

相反,它只显示没有图像的媒体播放器,并在播放按钮旁边显示一个旋转轮图标,直到单击时时间图标将从“--:--”更改为“-00:00”。然而,MP3 文件将加载,但媒体播放器(而不是显示时间)在播放按钮旁边显示“直播”。

我们的 html 本质上是:

<video width="500" height="500" controls autoplay>
   <source src='/path/to/route?param1=...'/>
</video>

或对于音频:

<audio preload='auto' controls='controls' autoplay='autoplay'>
   <source src='/path/to/route?param1=...' type='audio/type' />
</audio>

我们的控制器方法如下:

@GetMapping("/route")
fun doGet(@RequestParam(name = "param1") param1: String,
          ...,
          request: HttpServletRequest,
          response: HttpServletResponse) {

   //validate and pull audio/video files based on params and session
   ...
   // set response headers
   response.contentType = ...//get type of media
   var contentLength = ...//get length of all files you will be returning
   response.setContentLength(contentLength)
   response.setHeader("Content-Range", String.format("bytes 0-%d/%d", contentLength - 1, 
      contentLength))
   // Output Media
   response.outputStream.use { out ->
      mediaList.map { it.mediaData }
      .forEach { mediaBytes ->
         try {
            out.write(mediaBytes)
            out.flush()
          } catch (e: Exception) {
            log.error(e.message, e)
          }
       }
   }
}

我看到其他答案,内容范围:是 safari 上 wav 的必需标头,与其他浏览器不同,所以我添加它但无济于事。事实上,当查看 Safari 的开发窗口和网络窗口时,它表示获取媒体文件或响应的请求中没有附加标头。

有谁熟悉为什么请求中没有标头和/或如何在 Safari 上播放视频和 wav 文件?日志或控制台中没有抛出任何错误。

经过更多测试后,我想知道这是否是我们返回 Safari 不喜欢的媒体文件的方式。

I have been digging into a bug with our website where videos and wav type audio files are not loading/showing up when viewed through the Safari browser.

Instead it just displays the media player with no image and a spinning wheel icon next to the play button until clicked when the the time icon will change from '--:--' to '-00:00'. MP3 files however will load but the media player (instead of showing a time) displays 'Live Broadcast' next to the play button.

Our html is essentially:

<video width="500" height="500" controls autoplay>
   <source src='/path/to/route?param1=...'/>
</video>

or for audio:

<audio preload='auto' controls='controls' autoplay='autoplay'>
   <source src='/path/to/route?param1=...' type='audio/type' />
</audio>

and our controller method like:

@GetMapping("/route")
fun doGet(@RequestParam(name = "param1") param1: String,
          ...,
          request: HttpServletRequest,
          response: HttpServletResponse) {

   //validate and pull audio/video files based on params and session
   ...
   // set response headers
   response.contentType = ...//get type of media
   var contentLength = ...//get length of all files you will be returning
   response.setContentLength(contentLength)
   response.setHeader("Content-Range", String.format("bytes 0-%d/%d", contentLength - 1, 
      contentLength))
   // Output Media
   response.outputStream.use { out ->
      mediaList.map { it.mediaData }
      .forEach { mediaBytes ->
         try {
            out.write(mediaBytes)
            out.flush()
          } catch (e: Exception) {
            log.error(e.message, e)
          }
       }
   }
}

I saw other answers that content-range: was a required header for wav on safari unlike other browsers so I added that to no avail. In fact when looking at Safari's dev window and viewing the network window, it says that there are no headers attached to the request to get the media file, or the response.

Is anyone familiar with why there would be no headers on the request and/or how to get video and wav files playing on Safari? No errors are being thrown in the logs or the console.

After more testing I am wondering if it is how we are returning the media files that Safari does not like.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉态萌生 2025-01-16 03:49:05

经过大量挖掘并添加许多不同的标头等到响应中,我们找到了一个解决方案,我想我会将其发布给遇到此问题的任何其他人:

而不是像这样返回文件:按

response.outputStream.use { out ->
  mediaList.map { it.mediaData }
  .forEach { mediaBytes ->
     try {
        out.write(mediaBytes)
        out.flush()
      } catch (e: Exception) {
        log.error(e.message, e)
      }
   }}

如下方式返回它们:

return ResponseEntity.ok().contentType(MediaUtils.getMediaType(mediaType, mediaName)).body(byteArrayResource)

媒体类型将在哪里基于我们返回的文件。除了从其位置提取文件之外,不需要其他任何东西。希望这对遇到此问题的任何人都有帮助。

After much digging and adding many different headers and such to the response we figured out a solution and I thought I would post it for any others encountering this issue:

Instead of returning the files before like:

response.outputStream.use { out ->
  mediaList.map { it.mediaData }
  .forEach { mediaBytes ->
     try {
        out.write(mediaBytes)
        out.flush()
      } catch (e: Exception) {
        log.error(e.message, e)
      }
   }}

return them as such:

return ResponseEntity.ok().contentType(MediaUtils.getMediaType(mediaType, mediaName)).body(byteArrayResource)

where the media type would be based on the file(s) we were returning. Apart from pulling the files from their locations nothing else was needed. Hope this helps anyone who encounteres this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文