homepage Welcome to WebmasterWorld Guest from 54.196.167.69
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Flash and Shockwave
Forum Library, Charter, Moderator: open

Flash and Shockwave Forum

    
Play multiple sounds simultaneously?
Already have a Flash/JavaScript sound player, working on last goal.
JAB Creations




msg:4253842
 6:51 am on Jan 16, 2011 (gmt 0)

I've put together a Flash/JavaScript sound player where I can call sound files with JavaScript and have Flash execute this across all platforms. I can even send a stop signal to stop whatever sound is playing at the moment.

I'm now trying to figure out how to play multiple sound files simultaneously. So far I've read that it comes down to associating the file with a different object. The problem is I'm not sure how to check if an object is currently associated with a sound, if the sound disassociates after playing or not and if it doesn't how to disassociate it to clear it up for more sounds. I could pass another parameter from JavaScript with a global variable and have multiple channels in the Flash object however that seems kind of working around the problem instead of dealing with it. Suggestions please?

- John

Flash Scripting
import flash.external.*;
ExternalInterface.addCallback('Sound', null, playsound);
ExternalInterface.addCallback("Stop", null, stopsound);

function playsound(url)
{
sound = new Sound(soundLoader);
sound.loadSound(url,true);
}

function stopsound() {sound.stop();}



JavaScript
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Sound Player</title>
<script type="text/javascript">
//<![CDATA[
function play(url)
{
var splayer = document.getElementById('splayer');

if (splayer)
{
if (url=='stop' && splayer.Stop!=undefined) {splayer.Stop();}
else if (splayer.Sound!=undefined) {splayer.Sound(url);}
else {alert('Undefined, is height and width both at least 1px?');}
}
}
//]]>
</script>
</head>

<body>

<div><a href="javascript:play('stones.mp3');">play mp3 sound</a></div>
<div><a href="javascript:play('stop');">stop mp3 sound</a></div>

<object data="sound.swf" id="splayer" style="height: 1px; width: 1px;" type="application/x-shockwave-flash">
<param name="loop" value="false" />
<param name="play" value="true" />
<param name="quality" value="high" />
<param name="wmode" value="transparent" />
</object>

</body>
</html>

 

rocknbil




msg:4255251
 5:20 pm on Jan 19, 2011 (gmt 0)

Have you tried loading multiple sound objects? When you do this,

sound = new Sound(soundLoader);

You have a sound object named "sound". The first thing I'd try it to objectify it,

function playsound(soundname,url)
{
soundname = new Sound(soundLoader);
soundname.loadSound(url,true);
}
function stopsound(soundname) {soundname.stop();}

which is "bad" in that soundname, whatever it is, now becomes a global, but you're already doing that. You may have to use eval on the statements to get the name to apply correctly, or even use a global array and reference it anonymously.

The second thing you might have to do is dynamically create a layer holding the multiple objects, it depends on how your player is set up.

Last, you have only one external reference to the player.

splayer.Stop
splayer.Sound

You'll have to figure out a way to pass references to multiple sound objects.

One side note, this is intended to check if the object exists in the page and will error if it doesn't:

{
var splayer = document.getElementById('splayer');
if (splayer)
{

change to

{
if (document.getElementById('splayer'))
{
splayer = document.getElementById('splayer');

The logic being, check for it first before assigning the object to the variable, not the other way around.

JAB Creations




msg:4256753
 9:05 pm on Jan 22, 2011 (gmt 0)

Thanks for your input. I decided to use multiple static events as I'm not a Flash guru. I would love to make the Flash scripting more dynamic as well as figure out how to have Flash accept multiple parameters from JavaScript without having to resort to using split. I'm not a Flash guru so that will require input from others as I searched for hours without finding those answers though the SWF is only half a kilobyte so that's not too bad. The code below does work at least. :) I'll implement a global JavaScript variable to track which object number to call the Flash object with once I implement it.

- John

JavaScript
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Sound Player</title>
<script type="text/javascript">
//<![CDATA[
function play(url)
{
var splayer = document.getElementById('splayer');

if (splayer)
{
if (url=='stop' && splayer.Stop!=undefined) {splayer.Stop();}
else if (splayer.Sound!=undefined) {splayer.Sound(url);}
else {alert('Undefined, is height and width both at least 1px?');}
}
}
//]]>
</script>
</head>

<body>

<div><a href="javascript:play('sound1.mp3&1');">play mp3 sound 1</a></div>
<div><a href="javascript:play('sound2.mp3&2');">play mp3 sound 2</a></div>
<div><a href="javascript:play('stop');">stop mp3 sound</a></div>

<object data="sound2-final.swf" id="splayer" style="height: 1px; width: 1px;" type="application/x-shockwave-flash">
<param name="loop" value="false" />
<param name="play" value="true" />
<param name="quality" value="high" />
<param name="wmode" value="transparent" />
</object>

</body>
</html>


Flash Scripting
import flash.external.*;
ExternalInterface.addCallback('Sound',null,playsound);
ExternalInterface.addCallback("Stop", null, stopsound);

function playsound(param)
{
var p = param.split('&');
if (p[1]=='0') {sound0 = new Sound(sound0); sound0.loadSound(p[0],true);}
else if (p[1]=='1') {sound1 = new Sound(sound1); sound1.loadSound(p[0],true);}
else if (p[1]=='2') {sound2 = new Sound(sound2); sound2.loadSound(p[0],true);}
else if (p[1]=='3') {sound3 = new Sound(sound3); sound3.loadSound(p[0],true);}
else if (p[1]=='4') {sound4 = new Sound(sound4); sound4.loadSound(p[0],true);}
else if (p[1]=='5') {sound5 = new Sound(sound5); sound5.loadSound(p[0],true);}
else if (p[1]=='6') {sound6 = new Sound(sound6); sound6.loadSound(p[0],true);}
else if (p[1]=='7') {sound7 = new Sound(sound7); sound7.loadSound(p[0],true);}
}

function stopsound()
{
sound0.stop();
sound1.stop();
sound2.stop();
sound3.stop();
sound4.stop();
sound5.stop();
sound6.stop();
sound7.stop();
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Flash and Shockwave
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved