简体   繁体   English

Javascript:在多个标签中播放一次音频

[英]Javascript: Play audio once in multiple tabs

In my application I programmed that the system plays an audio to notify the user when he received a notification.在我的应用程序中,我编写了系统在用户收到通知时播放音频以通知用户的程序。

If the user have a lot of browser tabs opened, this audio is played a lot of times (one for tab).如果用户打开了很多浏览器选项卡,则此音频会播放很多次(一个用于选项卡)。

Is there any way to do that the audio only plays once ?有没有办法让音频只播放一次

Thank you!谢谢!

You could set a flag in localStorage : 您可以在localStorage设置一个标志:

//create random key variable
var randomKey = Math.random() * 10000;
//set key if it does not exist
if (localStorage.getItem("tabKey") === null) {
    localStorage.setItem("tabKey", randomKey);
}

This way, the item tabKey will be set to the randomKey of the first tab. 这样,项tabKey将被设置为第一个选项卡的randomKey Now, when you play the audio, you can do: 现在,当您播放音频时,您可以:

if (localStorage.getItem("tabKey") === randomKey) {
    playAudio();
}

This will play the audio only in the first tab. 这将仅在第一个选项卡中播放音频。

The only problem is, you have to react to the case, that the user is closing the first tab. 唯一的问题是,您必须对案例作出反应,即用户正在关闭第一个标签。 You can do this by unsetting the tabKey item on tab close and catching this event in other tabs with the storage event: 您可以通过tabKey设置选项卡上的tabKey项并使用storage事件在其他选项卡中捕获此事件来执行此操作:

//when main tab closes, remove item from localStorage
window.addEventListener("unload", function () {
    if (localStorage.getItem("tabKey") === randomKey) {
        localStorage.removeItem("tabKey");
    }
});

//when non-main tabs receive the "close" event,
//the first one will set the `tabKey` to its `randomKey`
window.addEventListener("storage", function(evt) {
    if (evt.key === "tabKey" && evt.newValue === null) {
        if (localStorage.getItem("tabKey") === null) {
            localStorage.setItem("tabKey", randomKey);
        }
    }
});

I modified @basilikum answer to keep the full array of tabs in storage.我修改了@basilikum 答案以将完整的选项卡数组保留在存储中。 Allows you to skip one of the listeners.允许您跳过其中一个听众。

 class TabManager { constructor() { this.tabKey = Math.random() * 10000; this.setup(); } getAllTabs() { return JSON.parse(localStorage.getItem("tabKeys") || '[]'); } setup() { const t = this; const currentTabs = this.getAllTabs(); currentTabs.push(this.tabKey); localStorage.setItem("tabKeys", JSON.stringify(currentTabs)); window.addEventListener("unload", function () { const currentTabs = t.getAllTabs(); const index = currentTabs.indexOf(t.tabKey) if (index > -1) { currentTabs.splice(index, 1); localStorage.setItem("tabKeys", JSON.stringify(currentTabs)); } }); } isMainTab() { return this.getAllTabs()[0] === this.tabKey; } }

Call this once in your code:在您的代码中调用一次:

const tabManager = new TabManager();

Call this whenever you want to do something on one tab:每当您想在一个选项卡上做某事时调用它:

if(tabManager.isMainTab()){
    //play sound...
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM