1. Create a sound instance.
sound = Components.classes["@mozilla.org/sound;1"] .createInstance(Components.interfaces.nsISound);2. Beep.
sound.beep();That is how simple it can be. The Take A Break extension beeps every 3 seconds during the short break -- while the icon is flashing. So the user can close his/her eyes for a rest. When the beeping stops, the user can know the short break is over.
To do beeping every 3 seconds, we use the setTimeout() function. First, define a function doBeep().
function doBeep()
{
if (sound) // the "sound" instance we created
{
// Get the current time in milli-seconds.
var currentTimeMs = (new Date()).getTime();
// flashEndTimeMs tells when the short break will finish.
// flashEndTimeMs was set before this function is called.
if (currentTimeMs > flashEndTimeMs)
{
// Flashing ends. Stop beeping.
beepTimer = null;
return;
}
sound.beep();
// restart timer.
// After 3 seconds, this function is called back again.
beepTimer = window.setTimeout(function(){ doBeep(); },
3000); // 3 seconds
}
}
As you can see, inside the function doBeep(), we register itself as a callback function for the 3 seconds timeout event. So doBeep() runs every 3 seconds until the short break ends.Secondly, when it is time for the short break, we call the function doBeep() to start the first beeping.
// Set the end time for the short break. var currentTimeMs = (new Date()).getTime(); flashEndTimeMs = secOfFlash * 1000 + currentTimeMs; // Start flashing and beeping. doFlash(); doBeep();This feature will be released in version 1.1 of Take A Break. The sample code here is modified a little to make it more understandable.
1 comment:
nsISound won't give you cross-platform browser compatibility. For example, good luck relying on nsISound to play a WAV file on Linux.
Post a Comment