navigator.mediasession.metadata不更新页面后重新加载

发布于 2025-02-13 14:43:31 字数 1572 浏览 1 评论 0原文

我目前正在将我们的音频播放器与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 技术交流群。

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

发布评论

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

评论(1

邮友 2025-02-20 14:43:31

搜索了几天后找不到任何东西。
我发现了一个解决方法。它与performance.navigation.type首次打开页面时,它设置为0,然后将其设置为1。我很好奇是否对<<有任何影响代码> MediaSession 因此,我通过使用window.open(window.location.href,“ _self”)弄清楚如何“硬刷新”页面,因为显然是生成生成的一个“硬刷新”。之后,MediaSession再次工作。因此,我所做的是设置一个on load功能,该功能检查performance.navigation.navigation.type == 1,如果是这样,

    window.onload = function() {
    
        if (performance.navigation.type != 0) {
    
            window.open(window.location.href, "_self");
    
        }
    };

请在用户刷新时进行“硬 刷新”刷新”。
还要确保移动窗口。 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 the mediasession so I figured out how to "Hard Refresh" the page by using window.open(window.location.href, "_self");, because apparently that generates a "Hard Refresh". After that the mediasession works again. so what I did was setup an onload function that checks to see if performance.navigation.type == 1 and if so then do a "Hard Refresh"

    window.onload = function() {
    
        if (performance.navigation.type != 0) {
    
            window.open(window.location.href, "_self");
    
        }
    };

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

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