navigator.mediasession.metadata不更新页面后重新加载
我目前正在将我们的音频播放器与MediaSession绑定。 一切正常,当我打播放并更新navigator.mediasession.metadata
时,它在台式机和移动设备上的通知中正确显示。
但是,在我重新加载页面并命中播放之后,该通知始终具有默认值(网站URL为标题和link rel = rel =“ icon”
for Artwork)。这只有在我重新加载网站后才发生。如果我关闭并再次打开通知,该通知再次正常工作。
完成操作:
//...
initialConfiguration: {
title: 'Initial Title',
artist: 'Initial Artist',
album: '',
artwork: [
{ src: "initial/artwork/url.jpg", sizes: "512x512", type: "image/jpg" },
]
},
currentMetadata: null,
setMediaSessionMetaData: function(){
let self = this;
if ('mediaSession' in navigator) {
if( !self.currentMetadata ){
self.currentMetadata = new MediaMetadata(self.initialConfiguration);
}else{
// Update existing metadata
self.currentMetadata.title = "New Title";
self.currentMetadata.artist = "New Artist";
self.currentMetadata.artwork = [
{ src: "new/artwork/url.jpg", sizes: '512x512', type: "image/jpg" },
];
}
navigator.mediaSession.metadata = self.currentMetadata;
}
},
//...
此功能在首页加载上的工作正常,当我第一次打播放时,它加载了initialconfiguration
,如果我再次调用该功能,则标题和艺术品会更新。但是在重新加载后,通知始终默认值忽略了我的配置。
MediaSession
中是否有一个错误,我在MediaSession Github Page上没有发现任何有关此问题的信息( https://github.com/w3c/mediasession/issues )并搜索此问题使我的结果为零。
I am currently tying our audio player with mediaSession.
Everything is working as it should, when I hit play and update navigator.mediaSession.metadata
, it's properly displayed in the notification on desktop and mobile.
But after I reload the page and hit play, the notification has always default values (website URL as a title and link rel="icon"
for the artwork). This only happens after I reload the website. If I close it and open again the notification is working properly again.
Here's how it's done:
//...
initialConfiguration: {
title: 'Initial Title',
artist: 'Initial Artist',
album: '',
artwork: [
{ src: "initial/artwork/url.jpg", sizes: "512x512", type: "image/jpg" },
]
},
currentMetadata: null,
setMediaSessionMetaData: function(){
let self = this;
if ('mediaSession' in navigator) {
if( !self.currentMetadata ){
self.currentMetadata = new MediaMetadata(self.initialConfiguration);
}else{
// Update existing metadata
self.currentMetadata.title = "New Title";
self.currentMetadata.artist = "New Artist";
self.currentMetadata.artwork = [
{ src: "new/artwork/url.jpg", sizes: '512x512', type: "image/jpg" },
];
}
navigator.mediaSession.metadata = self.currentMetadata;
}
},
//...
This function works perfectly fine on first page load, when I hit play for the first time it loads the initialConfiguration
and if I call the function again the title and artwork gets updated. But after reload, the notification has always default values ignoring my configuration.
Is there a bug in mediaSession
, I didn't find anything regarding this issue on mediaSession github page (https://github.com/w3c/mediasession/issues) and searching this issue gives me zero results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
搜索了几天后找不到任何东西。
我发现了一个解决方法。它与
performance.navigation.type
首次打开页面时,它设置为0,然后将其设置为1。我很好奇是否对<<有任何影响代码> MediaSession 因此,我通过使用window.open(window.location.href,“ _self”)弄清楚如何“硬刷新”页面,因为显然是生成生成的一个“硬刷新”。之后,
MediaSession
再次工作。因此,我所做的是设置一个on load功能,该功能检查performance.navigation.navigation.type == 1
,如果是这样,请在用户刷新时进行“硬 刷新”刷新”。
还要确保移动
窗口。 Onload
行到JavaScript文件的末尾或初始函数之后After Searching for days and not finding anything....
I discovered a workaround. It has to do with the
performance.navigation.type
when you first open a page it is set to 0 and after a refresh it gets set to 1. I was curious if this had any effect on themediasession
so I figured out how to "Hard Refresh" the page by usingwindow.open(window.location.href, "_self");
, because apparently that generates a "Hard Refresh". After that themediasession
works again. so what I did was setup an onload function that checks to see ifperformance.navigation.type == 1
and if so then do a "Hard Refresh"Now when the user refresh's it will do a "Hard Refresh".
Also make sure to move the
window. Onload
line to the end of the javascript file or after the initial function