PDA

View Full Version : Javascripting Sound Objects


kaitana
05-13-2003, 10:16 PM
Hello Guys, I have a really tough question, and no other board has been able to answer this one.

The website is a 'make me talk' website with a bunch of sound bite 'words' in a databse, and the user types in a sentence, and the cheracter talks.

The script I am having trouble with is looping through the embed tags, and playing the sounds in order. This is how it works so far:

I use embed tags to preload the files:
<embed name='zero' src = 'zero.wav' hidden='true' type='audio/wav' loop='1' AUTOSTART='FALSE'>
<embed name='one' src = 'one.wav' hidden='true' type='audio/wav' loop='1' AUTOSTART='FALSE'>

I then create objects to emulate the embed tags:

function soundbite(name, length)
{
this.name = name;
this.src = name + ".wav";
this.hidden = true;
this.type = 'audio/wav';
this.loop = 1;
this.autostart = false;
this.length = length;
}
var bites = new Array();
bites[0] = new soundbite('zero', 490);
bites[1] = new soundbite('one', 640);

now is the tough part, and this is what I cannot seem to make work. I beed to use a setTimeout function to call the playit() function so the words are spaced properly:

setTimeout("playit(bites[0]);", bites[0].length);

the tough part is how do I access the document.embeds array to call the play() function?

//this function does not work..
function playIt(obj){
var snd = obj.name
alert(eval(document.embeds[snd]));
eval(document.embeds[snd]).play(true, obj.src)
}

This is a tough one. If anyone can help, I would be in debt. :thumbsup:

glenngv
05-14-2003, 04:05 AM
try:

function playIt(obj){
alert(obj);//test if it is really object
document.embeds[obj.name].play(true, obj.src);
}

kaitana
05-14-2003, 05:12 AM
Yes, that was first attempt. the error says:

Error:'document.embeds[...]' is null or not an object

which is why I tried using the eval function..

I do not thing the embeds array is being accessed correctly

cheesebagpipe
05-14-2003, 06:16 AM
Hmmm...why use an object? All that data is already held in the Embed objects; all you really need is the name of the element (to call its .play() method), and the delay in ms. Maybe:

var bites = new Array();
bites[0] = { name: 'zero', length: 490 };
bites[1] = { name: 'one', length: 640 };
bites[2] = { name: 'two', length: 400 };

function playIt() {
var spacing = 0, bitesLen = bites.length;
for (var i=0; i<bitesLen; ++i) {
setTimeout('document.embeds["'+bites[i].name+'"].play()', spacing);
spacing += bites[i].length;
}
}

Just a wild guess...:confused:

kaitana
05-14-2003, 06:43 AM
same error occurs
Error:'document.embeds[...]' is null or not an object

anyone know where I can find info on the embeds array?

cheesebagpipe
05-14-2003, 06:47 AM
How are you calling the function? Post a url if you have one.

kaitana
05-14-2003, 07:25 AM
http://www.tallgrrl.com/giant/test.html

cheesebagpipe
05-14-2003, 05:45 PM
Should have thought of this earlier :o but you can't call a method of an embedded object until it's embedded:

<body onload="playIt()">

If that works, I'd skip the constructor (unless you need it for something else) and just use the (more elegant, imo) object literal notation:

var bites = new Array();
bites[0] = { name: 'zero', length: 490 };
bites[1] = { name: 'one', length: 640 };
bites[2] = { name: 'two', length: 400 };

Just like CSS...

kaitana
05-14-2003, 06:18 PM
cheese, I appreciate the effort, I really do.
but there is no issues with the constructors.

bites[0] = { name: 'zero', length: 490 };
works as good as:
bites[0] = new soundbite('zero', 490);

the issue is with the embeds array:

document.embeds["zero"].play();
document.embeds[0].play();

neither work. all the documentation says it should.