Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

play and stop sound with 1 button instead of two--help pls 1

Status
Not open for further replies.

FunWithNortel

Technical User
Oct 24, 2006
82
US
Looking for a way to start and stop my sound file with only one button. Currently using two. The play code came from Dreamweaver and I edited it to make a similar puase button. Its kind of an eyesore to have two buttons and was wondering if anyone could help.


___________________________________________________________
function MM_controlSound2(x, _sndObj, sndFile) { //v3.0
var i, method = "", sndObj = eval(_sndObj);
if (sndObj != null) {
if (navigator.appName == 'Netscape') method = "pause";
else {
if (window.MM_WMP == null) {
window.MM_WMP = false;
for(i in sndObj) if (i == "ActiveMovie") {
window.MM_WMP = true; break;
} }
if (window.MM_WMP) method = "pause";
else if (sndObj.FileName) method = "run";
} }
if (method) eval(_sndObj+"."+method+"()");
else window.location = sndFile;
}

function MM_controlSound(x, _sndObj, sndFile) { //v3.0
var i, method = "", sndObj = eval(_sndObj);
if (sndObj != null) {
if (navigator.appName == 'Netscape') method = "play";
else {
if (window.MM_WMP == null) {
window.MM_WMP = false;
for(i in sndObj) if (i == "ActiveMovie") {
window.MM_WMP = true; break;
} }
if (window.MM_WMP) method = "play";
else if (sndObj.FileName) method = "run";
} }
if (method) eval(_sndObj+"."+method+"()");
else window.location = sndFile;
}

____________________________________________________________

Embedded it with this code


<EMBED NAME='CS1153613119336' SRC='Sounds/Mellow Mist.mp3' LOOP=true
AUTOSTART=true MASTERSOUND HIDDEN=true WIDTH=0 HEIGHT=0></EMBED>


 
Assuming you call "MM_controlSound" when you want to play, and "MM_controlSound2" when you want to pause with 3 parameters, you can now call "playPause" with the same three parameters, plus a fourth which is either the boolean value true to play the sound, or the boolean value false to pause it:

Code:
function playPause(x, _sndObj, sndFile, play) {
	var i, method = '', sndObj = eval(_sndObj);
	if (sndObj != null) {
		if (navigator.appName == 'Netscape')
			method = (play) ? 'play' : 'pause';
		else {
			if (window.MM_WMP == null) {
				window.MM_WMP = false;
				for(i in sndObj)
					if (i == "ActiveMovie") {
						window.MM_WMP = true;
						break;
					}
			}

			if (window.MM_WMP)
				method = (play) ? 'play' : 'pause';
			else if (sndObj.FileName)
				method = 'run';
		}
	}

	if (method)
		eval(_sndObj+'.'+method+'()');
	else
		window.location = sndFile;
}

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I tried the code and it would stop the music but would not start again.

scenario:

page loads and the sound file plays the music in a loop.

<EMBED NAME='CS1153613119336' SRC='Sounds/Mellow Mist.mp3' LOOP=true
AUTOSTART=true MASTERSOUND HIDDEN=true WIDTH=0 HEIGHT=0></EMBED>


I have a button that is set to on click call the function

<div id="Layer50"><img src="Buttons/music.JPG" alt="Play Music" width="98" height="109" onclick="playPause('play','document.CS1153613119336','Sounds/Mellow Mist.mp3')" /></div>

The function

function playPause(x, _sndObj, sndFile, play) {
var i, method = '', sndObj = eval(_sndObj);
if (sndObj != null) {
if (navigator.appName == 'Netscape')
method = (play) ? 'play' : 'pause';
else {
if (window.MM_WMP == null) {
window.MM_WMP = false;
for(i in sndObj)
if (i == "ActiveMovie") {
window.MM_WMP = true;
break;
}
}

if (window.MM_WMP)
method = (play) ? 'play' : 'pause';
else if (sndObj.FileName)
method = 'run';
}
}

if (method)
eval(_sndObj+'.'+method+'()');
else
window.location = sndFile;
}


Thanks for your help thus far it is greatly apreciated.
 
Perhaps you should re-read the instructions I gave:

self said:
you can now call "playPause" with the same three parameters, [!]plus a fourth which is either the boolean value true to play the sound, or the boolean value false to pause it[/!]

I didn't test the code, but I can guarantee that it won't work properly without that parameter.

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I understand now. I am less than a novice at these things. How do I add the 4th parameter and make it work? Thanks for your time.
 
Change this:

Code:
onclick="playPause('play','document.CS1153613119336','Sounds/Mellow Mist.mp3')"

by adding a fourth parameter of either true or false, i.e.

Code:
onclick="playPause('play', 'document.CS1153613119336', 'Sounds/Mellow Mist.mp3'[!], true[/!])"

or

Code:
onclick="playPause('play', 'document.CS1153613119336', 'Sounds/Mellow Mist.mp3'[!], false[/!])"

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
And to do this with only one button, you can have a global variable "playState" (or whatever) which is initialised to true (or false), and pass that through, toggling it each time:

Code:
onclick="[!]playState = !playState;[/!] playPause('play', 'document.CS1153613119336', 'Sounds/Mellow Mist.mp3'[!], playState[/!])"

Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top