Author: |
Thinkhy |
Date: |
2010.04.21. |
URL: |
Http://www.disandu.com /? P = 731 |
Keyword: |
JS switch background music JS control background music Javascript |
On the Internet, most of the methods for switching between JS and background music are not feasible. Finally, a solution is found on a Korean website, which requires a delay when switching music.
Musicstatus = false;
// Background music, playing specific tracks, empty tracks, and pausing music
Function playmusicfile (musicfile)
{
VaR objmusic = Document. getelementbyid ("bgmusic ");
If (! Objmusic)
Return;
If (musicfile = "") // the processing of these rows. When playing a blank file, the music will be stopped after a delay of some time.
{
If (typeof objmusic. Pause = 'function ')
{
Objmusic. Pause (); // when an empty file is encountered, close the previous music.
}
}
VaR parele = objmusic. parentnode;
Parele. removechild (objmusic );
VaR newele = Document. createelement ("embed ");
Newele. setattribute ("ID", "bgmusic ");
Newele. src = musicfile;
Newele. setattribute ("hidden", "true ");
Newele. setattribute ("autostart", "false ");
Parele. appendchild (newele );
If (musicstatus = "pause ")
{
SetTimeout (musicstop, 300); // note that a delay is required to wait for the background music file to be loaded into the memory.
}
Else if (musicstatus = "play ")
{
SetTimeout (musicplay, 300 );
}
// Objmusic = newele;
}
Function musicstop (){
If (bgmusic & (typeof bgmusic. Stop = 'function'
| Typeof bgmusic. Stop = 'unknown ') // sometimes bgmusi. Stop is of the unknown type.
Bgmusic. Stop ();
}
Function musicplay (){
If (bgmusic & typeof bgmusic. Play = 'function'
| Typeof bgmusic. Play = 'unknown ') // sometimes bgmusi. Play is of the unknown type.
Bgmusic. Play ();
}