[Experience Summary] JS Code for switching background music

Source: Internet
Author: User

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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.