Connecting Tech Pros Worldwide Forums | Help | Site Map

Music in background w/ onload, function, or any other method.

Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#1: Sep 14 '07
I need a function that plays a song in the background ( in other words the user can't stop/start the music they have to go to another page or listen to it )

I'm not the greatest at JavaScript I don't do much with it, but I'm hoping to change that.

It doesn't really make a difference if you can do it wile the page is loading or it starts when the page is completely loaded.

Thanks, Death

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,750
#2: Sep 15 '07

re: Music in background w/ onload, function, or any other method.


That would be annoying, but here's a link.
Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#3: Sep 17 '07

re: Music in background w/ onload, function, or any other method.


Quote:

Originally Posted by acoder

That would be annoying, but here's a link.

The type of site I'm making, the users will expect music, so it wont be "annoying", and I've been to w3schools, already thats why I'm asking for a way to put it into a function.

Thanks anyway, Death
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,750
#4: Sep 18 '07

re: Music in background w/ onload, function, or any other method.


Quote:

Originally Posted by Death Slaught

The type of site I'm making, the users will expect music, so it wont be "annoying"

Maybe that should've been "could be".

I don't play much with sound, but you could try using an iframe linked to a sound file. Do you mind the sound player showing on the screen?
Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#5: Sep 18 '07

re: Music in background w/ onload, function, or any other method.


Quote:

Originally Posted by acoder

Maybe that should've been "could be".

I don't play much with sound, but you could try using an iframe linked to a sound file. Do you mind the sound player showing on the screen?

Yea I wan't it to be in the background.

Thanks, Death
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,750
#6: Sep 19 '07

re: Music in background w/ onload, function, or any other method.


Quote:

Originally Posted by Death Slaught

Yea I wan't it to be in the background.

Thanks, Death

Have a play with this. Instead of playing using a button, you could add it onload.
Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#7: Sep 19 '07

re: Music in background w/ onload, function, or any other method.


Quote:

Originally Posted by acoder

Have a play with this. Instead of playing using a button, you could add it onload.

Thanks, but it's still not what I'm looking for, I wan't the music to play in the background yes, but I wan't it in a JavaScript function, onload would be fine as long as it's something like this. (no this doesn't work it's just something i've tried).



Expand|Select|Wrap|Line Numbers
  1. function play()
  2.  
  3. {
  4. window.bgsound="location";
  5. }

[HTML]<body onload="play()">
</body>[/HTML]

Thanks again, Death
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,750
#8: Sep 20 '07

re: Music in background w/ onload, function, or any other method.


Quote:

Originally Posted by Death Slaught

Thanks, but it's still not what I'm looking for, I wan't the music to play in the background yes, but I wan't it in a JavaScript function, onload would be fine as long as it's something like this. (no this doesn't work it's just something i've tried).

Have you tried something like this:
Expand|Select|Wrap|Line Numbers
  1. function play() {
  2. embed = document.createElement("embed");
  3. embed.setAttribute("src", "soundfile.wav");
  4. embed.setAttribute("hidden", true);
  5. embed.setAttribute("autostart", true);
  6. document.body.appendChild(embed);
  7. }
and call it onload.
helimeef's Avatar
Member
 
Join Date: Sep 2007
Posts: 62
#9: Sep 20 '07

re: Music in background w/ onload, function, or any other method.


Jeez okay, the last reply was probably the best recommendation. But just a few issues with making music play in the background:
1. It must not be copyrighted, or you must be the owner.
2. You better put play/pause and next/previous button, or else your users will get annoyed with the music constantly playing and restarting every time they refresh.
3. Although your users will expect to hear music, your looking at a rare audience. What about visitors? Stumblers? They have no idea your site will play music, unless you put "THIS SITE PLAYS MUSIC" in your title tag :)
Good luck.
Death Slaught's Avatar
Site Addict
 
Join Date: Aug 2007
Location: Tennessee
Posts: 952
#10: Sep 20 '07

re: Music in background w/ onload, function, or any other method.


Quote:

Originally Posted by acoder

Have you tried something like this:

Expand|Select|Wrap|Line Numbers
  1. function play() {
  2. embed = document.createElement("embed");
  3. embed.setAttribute("src", "soundfile.wav");
  4. embed.setAttribute("hidden", true);
  5. embed.setAttribute("autostart", true);
  6. document.body.appendChild(embed);
  7. }
and call it onload.

Thanks just what I needed.

- Death
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,750
#11: Sep 21 '07

re: Music in background w/ onload, function, or any other method.


Quote:

Originally Posted by helimeef

Jeez okay, the last reply was probably the best recommendation. But just a few issues with making music play in the background:
1. It must not be copyrighted, or you must be the owner.
2. You better put play/pause and next/previous button, or else your users will get annoyed with the music constantly playing and restarting every time they refresh.
3. Although your users will expect to hear music, your looking at a rare audience. What about visitors? Stumblers? They have no idea your site will play music, unless you put "THIS SITE PLAYS MUSIC" in your title tag :)
Good luck.

Good points. I'd say a pause/stop button wouldn't do any harm.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,750
#12: Sep 21 '07

re: Music in background w/ onload, function, or any other method.


Quote:

Originally Posted by Death Slaught

Thanks just what I needed.

- Death

Glad to hear you got it working. Remember to take care with sound.
Reply