Reputation: 781
I have Chrome Extension that consists of a popup and an Event page that runs in the background. The Event page is really simple: it registers an onMessage
listener and when that listener is called, it creates an Audio
object with a remote .mp3 file as the source and plays it. When the Event page gets another message and audio is playing, it pauses the audio and stops the play back.
var audio;
chrome.runtime.onMessage.addListener( function(message, sender, sendResponse) {
if (audio && !audio.paused) {
audio.pause();
audio.src = '';
audio.load();
audio = null;
sendResponse({});
} else {
audio = new Audio("http://path/to/audio/file.mp3");
audio.play();
sendResponse({});
}
});
The popup jump simply sends a message to the Event page to start playing the audio. The audio plays fine and stops playing when the Event page receives the second message, but the issue is that when the Event page receives the second message, pauses, and sets the audio source to an empty String
, the Event page never goes away. It will persist in the background like a Background
page and never goes into it's inactive state.
I tried creating the audio
var like this to see if using the Audio
constructor was causing the issue:
audio = document.createElement('audio');
But that did not behave any different. I then tried setting the preload
attribute to none
instead of auto
to no avail. I am wondering if Chrome is opening a socket to load the mp3 file and is not closing it properly, so the Event page still thinks that it is active because of the open socket, but that is just speculation at this point...
Is there something else that I need to do to "unload" the audio so that the Event page will go into an inactive state when audio is not playing?
Upvotes: 2
Views: 162
Reputation: 781
After upgrading Chrome to version 36, the Event page goes inactive after the Audio element is unloaded.
Upvotes: 2