Help - Search - Members - Calendar
Full Version: Embedding sound using JS Vars?
HTMLHelp Forums > Programming > Client-side Scripting
WaWb
How do i use the < embed src="SOMETHING" > with a JavaScript var, say, sound..?

I mean, if i've already set sound = 'mooh'; , how do i < embed src=sound > -> < embed src='mooh' > ???? I'm hoping you can understand it wink.gif
pandy
This is a little dated, but maybe you can get some ideas there.
http://irt.org/script/sound.htm
WaWb
Alright, i've now looked that page through, and it doesn't say anything about my problem..! Whenever it uses the embed src=-thing, it sets it to a sound file, not a variable like i want it to...

MY PROBLEM:

I have a folder with some music files in it, put so that there's a master folder (Music), which contains folders with artists (Kraftwerk, Boys Noize, MGMT), which again contains the music which the artist has made (Apples.mp3, Song.mp3, MonkeyBoat.mp3) , now, if i put the .html file in the master folder, and could get the variable SOUND, which could be for an example: 'MGMT/Kids.mp3', i should be able to play it using < embed src=SOUND > which SHOULD (it isn't) be like < embed src='MGMT/Kids.mp3' > . NOW, i want it to work smile.gif So that i CAN use the embed src with a variable (SOUND) instead of the path itself ('MGMT/Kids.mp3')...??

Sooo, i have:
SOUND, the string containing the path 'MGMT/Kids.mp3'
A way of playing it (using the embed function)

What i don't have:
A way of playing SOUND, instead of playing the path directly.

I would be so fund of an answer! ;P -BTW, i know it's kind of a mess..smile.gif
Brian Chandler
I think the simple answer is: "You can't". I don't honestly know quite what _you_ mean by "a variable", but I'm not convinced you do either. If the embed tag in html requires a string (essentially, everything in html is a string) which is the name of the file to be accessed, then in a programming language with variables (either server script like PHP or client script like javascript), then if you have a variable holding the string value of the filename, then you can arrange for the variable to be replaced by the string value.

Everything in software is basically numbers, encoding all sorts of information. There is no actual "sound", any more than there are actually any "accented letters".

HTH
WaWb
Uhm, so if i have set (in JS) path = 'MGMT/Kids';, it wouldn't be a variable, it would be a string? Okay. So that means that i want to use the string called path with the < embed > -thing. Exactly how do i do that? Because i noticed that you wrote:

if you have a variable holding the string value of the filename, then you can arrange for the variable to be replaced by the string value.

Now, i'm thinking that you're actually saying that i can go from having the path variable, which contains the string 'MGMT/Kids', to having something i can use with the < embed > thing..? Or do you mean that i can replace the name of the variable, path, with the string that it holds so that i can use the name of the variable.........?? Thanks for the replies so far..wink.gif
Brian Chandler
QUOTE
Uhm, so if i have set (in JS) path = 'MGMT/Kids';, it wouldn't be a variable, it would be a string?


No, in this case path is a javascript variable, whose value is the string. (But how have you set this javascript variable? Unless it's in response to something happening in the browser, there's no point in not simply sending the name 'MGMT/Kids' in the html file... ?)

The simplest way of writing out bits of text (html) with javascript is the document.write(x) function, which writes x (or rather the string value of the variable x) at the current point in the document. But as I said, there's no much point in this, if the value is determined on the server. If you want to change the value dynamically, you have to poke through the javascript documentation to find out how to refer to the relevant part of the document structure. (Look up "DOM" -- document object model -- or somesuch)

Quite possibly you just need to generate your page dynamically on the server (which is less messy than javascript, imo)
WaWb
I've made two < form > s, one that says Artist [INPUT BOX] and one that says Title [INPUT BOX]. Now, using the "get" thing with the < form > thing, i've used JS to set the address (like www.google.com) to a variable, path, and then cut it down to 'WhatYouWroteInTheArtistInputBox/WhatYouWroteInTheTitleInputBox' , so if i wrote MGMT in the artist box and Kids.mp3 in the title box, the JS variable would contain the string 'MGMT/Kids.mp3' after the cut. Now if i could use that JS variable in the < embed > thing, i could actually play the song! BTW, i can use the path variable to write the string on the page using the mentioned document.write thing, but that's not gonna play the song in the browser, which is what i'm trying to achieve smile.gif
WaWb
Right, so i figured it out myself, here's how:

From JS Variable -> Play It In Your Default Media Player:

document.getElementById('[ID OF THE ELEMENT YOU WANT REPLACED BY THE DEFAULT MEDIA PLAYER]').innerHTML = [JS Variable Name];

Where [JS Variable Name] has to be = <embed src='[ARTIST]/[SONG NAME].[EXTENSION]'> </embed>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.