简体   繁体   English

当Workbox缓存mp3时,无法擦洗/滚动jPlayer音频

[英]Cannot scrub/scroll through jPlayer audio when mp3 is cached by Workbox

I have converted a single page HTML5 Cordova app into a PWA. 我已经将单页HTML5 Cordova应用程序转换为PWA。 The app uses jPlayer extensively to play mp3 files. 该应用广泛使用jPlayer播放mp3文件。 I am using a variant of the circular jPlayer here: http://jplayer.org/latest/demo-05/ . 我在这里使用圆形jPlayer的变体: http ://jplayer.org/latest/demo-05/。 The circle player has a circular progress bar that can also be used to scrub backwards and forwards through the track. 圈子播放器具有圆形进度条,该进度条也可以用于在轨道上前后滑动。

Everything works fine in PWA mode until I cache the mp3 with Workbox (version 4.3.1). 在PWA模式下,一切正常,直到我使用Workbox(版本4.3.1)缓存mp3。 Then scrubbing fails. 然后清理失败。 I can grab the scrub bar and move it but when I release it the track restarts from the beginning. 我可以抓住擦洗杆并移动它,但是当我松开它时,音轨将从头开始重新开始。 This happens if I use precaching for the mp3 or a dedicated audio cache for all mp3 files. 如果我对mp3使用预缓存,或者对所有mp3文件使用专用的音频缓存,则会发生这种情况。 Turn off caching, update the service worker and refresh -- and I can scrub. 关闭缓存,更新服务人员并刷新-我可以进行清理。 Turn on caching and refresh and scrubbing fails. 打开缓存和刷新,清理失败。

I would really like scrubbing to work with cached audio files so that the app can work offline. 我真的很想与缓存的音频文件协同工作,以便该应用程序可以脱机工作。

This seems similar in nature to Make mp3 seekable PHP . 这本质上与使mp3可查找PHP相似。

I'm excerpting this from the Workbox documentation's recipe for serving cached audio and video . 我是从Workbox文档的配方中摘录来提供缓存的音频和视频的

There are a few wrinkles in how some browsers request media assets (eg, the src of a <video> or <audio> element) that can lead to incorrect serving behavior unless you take specific steps when configuring Workbox. 除非您在配置Workbox时采取特定步骤,否则某些浏览器如何请求媒体资产(例如<video><audio>元素的src会出现一些皱纹,这可能会导致错误的投放行为。

Full details are available in this GitHub issue discussion ; 完整的详细信息可以在GitHub问题讨论中找到 a summary of the important points is: 要点总结如下:

  • Workbox must be told to respect Range request headers by adding in the workbox-range-requests plugin to the strategy used as the handler. 必须通过将workbox -range-requests插件添加到用作处理程序的策略中,告知Workbox遵守Range请求标头

  • The audio or video element needs to opt-in to CORS mode using the crossOrigin attribute , eg via . 音频或视频元素需要使用crossOrigin属性 (例如通过)加入CORS模式。

  • If you want to serve the media from the cache, you should explicitly add it to the cache ahead of time. 如果要从缓存中提供媒体,则应提前将其显式添加到缓存中。 This could happen either via precaching, or via calling cache.add() directly. 这可以通过cache.add()缓存或直接调用cache.add()来实现。 Using a runtime caching strategy to add the media file to the cache implicitly is not likely to work, since at runtime, only partial content is fetched from the network via a Range request. 使用运行时缓存策略将媒体文件隐式添加到缓存中不太可能起作用,因为在运行时,仅通过Range请求从网络获取部分内容。

Putting this all together, here's an example of one approach to serving cached media content using Workbox: 综上所述,这是使用Workbox服务缓存的媒体内容的一种方法的示例:

<!-- In your page: -->
<!-- You currently need to set crossOrigin even for same-origin URLs! -->
<video src="movie.mp4" crossOrigin="anonymous"></video>
// In your service worker:
// It's up to you to either precache or explicitly call cache.add('movie.mp4')
// to populate the cache.
//
// This route will go against the network if there isn't a cache match,
// but it won't populate the cache at runtime.
// If there is a cache match, then it will properly serve partial responses.
workbox.routing.registerRoute(
  /.*\.mp4/,
  new workbox.strategies.CacheFirst({
    cacheName: 'your-cache-name-here',
    plugins: [
      new workbox.cacheableResponse.Plugin({statuses: [200]}),
      new workbox.rangeRequests.Plugin(),
    ],
  }),
);

If you plan on precaching the media files, then you need to take an extra step to explicitly route things so that they're read from the precache, since the standard precache response handler won't use the range request plugins: 如果您打算预先缓存媒体文件,则需要采取额外的步骤来显式路由内容,以便从预缓存中读取它们,因为标准的预缓存响应处理程序将不使用范围请求插件:

workbox.routing.registerRoute(
  /.*\.mp4/,
  new workbox.strategies.CacheOnly({
    cacheName: workbox.core.cacheNames.precache,
    plugins: [
      new workbox.rangeRequests.Plugin(),
    ],
    // This is needed since precached resources may
    // have a ?_WB_REVISION=... URL param.
    matchOptions: {
      ignoreSearch: true,
    }
  }),
);

// List this *after* the preceding runtime caching route.
workbox.precaching.precacheAndRoute([...]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Firebase 提供时,带有范围请求路由器的 Workbox 预缓存音频无法在 Chrome 中播放 - Workbox pre-cached audio with range requests router fails to play in Chrome when served from Firebase 使用工作箱时如何从缓存的 url 中忽略 url 查询字符串? - How to ignore url querystring from cached urls when using workbox? 当资源未缓存且脱机时,Workbox 重定向客户端页面 - Workbox redirect the clients page when resource is not cached and offline 我如何知道服务工作者通过Workbox缓存的响应的日期时间? - How can I know the datetime of a response cached by a service worker through Workbox? 带工作箱的离子PWA-缓存字体不显示 - Ionic PWA with Workbox - Cached fonts not displaying iOS 15 上的 Workbox PWA 在离线时间歇性地显示“Safari 无法打开页面” - Workbox PWA on iOS 15 intermittently shows "Safari cannot open the page" when offline 离线时来自 Workbox 的 offlineFallback 不起作用 - offlineFallback from Workbox is not working when going offline 将工作箱与CDN一起使用时,缓存存储为空 - Cache storage is empty when using workbox with CDN 使用 Vue CLI 时如何更新最新的工作箱版本 - How to update the latest workbox version when using Vue CLI Vue Workbox Webpack “不能在模块外使用导入语句” - Vue Workbox Webpack “cannot use import statement outside a module”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM