Copy link to clipboard
Copied
I am trying to find a very good step by step tutorial on using "Flashvars" to load external mp3 files, I have been searching on google and the many I have found fall short in some way or another. From what I have been able to gather, I have tried using the code in red (commented out), which works, and modify it to use "Flashvars". The code that works, the mp3 file is hard coded, but my goal is to use "Flashvars" which I am attempting to do in the code following the code in red. I am also including the html code. Any help will be greatly appreciated.
Thanks,
David
/*
var soundReq:URLRequest = new URLRequest("mardi_gras2.mp3");
var sound:Sound = new Sound();
sound.load(soundReq);
sound.addEventListener(Event.COMPLETE, onComplete);
function onComplete(event:Event):void
{
sound.play();
}
*/
var soundReq:URLRequest = new URLRequest(root.loaderInfo.parameters.audio);
var sound:Sound = new Sound();
sound.load(soundReq);
sound.addEventListener(Event.COMPLETE, onComplete);
function onComplete(event:Event):void
{
sound.play();
}
HTML CODE
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" id="mySoundvars" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="mySoundvars.swf" />
<param name="FlashVars" value="audio=mardi_gras2.mp3" />
<param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /> <embed src="mySoundvars.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="mySoundvars" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" FlashVars="audio=mardi_gras2.mp3" />
Copy link to clipboard
Copied
You don't want to use URL request class.
You can got your Flahvar MP3 address like so:
//retrive your flashvars
var myFlashVars:Object = LoaderInfo(this.loaderInfo).parameters;
var myMP3Adress = myFlashVars.audio;
Copy link to clipboard
Copied
Waterlovinguy, below are the changes that I made. I am still not able to get sound...
//retrive your flashvars
var myFlashVars:Object = LoaderInfo(root.loaderInfo).parameters;
var myMP3Adress = myFlashVars.audio;
var sound:Sound = new Sound();
sound.load(myMP3Adress);
sound.addEventListener(Event.COMPLETE, onComplete);
function onComplete(event:Event):void
{
sound.play();
}
HTML CODE
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" id="mySoundvars" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="mySoundvars.swf" />
<param name="FlashVars" value="audio=mardi_gras2.mp3" />
<param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /> <embed src="mySoundvars.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="mySoundvars" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" FlashVars="audio=mardi_gras2.mp3" />
</object>
Copy link to clipboard
Copied
Here's a flushed out example. Note this is basically based on the live docs example found here:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/media/Sound.html
The only difference is getting the file name from a flash var (in bold), otherwise it's verbatim to the doc.
Should help you get going... I'm listening to my test song file now as I type this.
package {
import flash.display.Sprite;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.ID3Info;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.events.Event;
import flash.display.LoaderInfo;
public class Sound_id3Example extends Sprite {
private var snd:Sound = new Sound();
private var myTextField:TextField = new TextField();
public function Sound_id3Example() {
var myFlashVars:Object = LoaderInfo(this.loaderInfo).parameters;
var myMP3Adress = myFlashVars.audio;
snd.addEventListener(Event.ID3, id3Handler);
snd.load(new URLRequest(myMP3Adress));
}
private function id3Handler(event:Event):void {
var id3:ID3Info = snd.id3;
snd.play();
myTextField.autoSize = TextFieldAutoSize.LEFT;
myTextField.border = true;
myTextField.appendText("Received ID3 Info: \n");
for (var propName:String in id3) {
myTextField.appendText(propName + " = " + id3[propName] + "\n");
}
myTextField.appendText("\n" + "Artist: " + id3.artist + "\n");
myTextField.appendText("Song name: " + id3.songName + "\n");
myTextField.appendText("Album: " + id3.album + "\n\n");
this.addChild(myTextField);
}
}
}
Copy link to clipboard
Copied
The one other thing I forgot to mention was that in your code you attached your listener after you called the event you wanted to listen to. You can miss your event doing that. It's always a good idea to attach your listeners before you call the event, that way you are listening ahead of time.
So flip those two lines in your code:
sound.load(soundReq);
sound.addEventListener(Event.COMPLETE, onComplete);
becomes
sound.addEventListener(Event.COMPLETE, onComplete);
sound.load(soundReq);
And then I pointed you to the ActionScript Language reference because you were on the right track and all you needed was a good example. The language reference is full of good, proven examples.
Copy link to clipboard
Copied
waterlovinguy,
thank you for the update, and the code and reference you posted yesterday. I am still working on that code because I got an error: "1037: Packages cannot be nested." I am currently looking through some reference to figure it out... It seems that code should be in an actionscript (.as) file, am I correct? Or did you put that code into an (.fla) file?
Copy link to clipboard
Copied
That example code was contained in a .as file named 'Sound_id3Example.as' and it was set as the Document Class for an empty .fla. There was no ActionScript (or anything else) within the .fla. It was just blank.
If you are having package issues, you might want to peruse this to come up to speed:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9e.html
Copy link to clipboard
Copied
Ok waterlovinguy,
I went back to my original code, and I got things to work... I think my whole issue was with the HTML CODE. So I decided to use the "SWFObject generator". I had never used it before, frankly I had never heard of it until I started my searches on "Flashvars".
Thank you very much for all your assistance, PEACE,
David
Final Codes below
var my_var:String = new String();
my_var = root.loaderInfo.parameters.myflashvar;
myText.text = my_var;
var soundReq:URLRequest = new URLRequest(root.loaderInfo.parameters.audio);
var sound:Sound = new Sound();
var soundControl:SoundChannel = new SoundChannel;
sound.load(soundReq);
play_btn.addEventListener(MouseEvent.CLICK, playSound);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
function playSound(event:MouseEvent):void
{
SoundMixer.stopAll();
soundControl = sound.play();
}
function stopSound(event:MouseEvent):void
{
soundControl.stop();
}
HTML Code
<!<![endif]>
</a>
</object>
<!<![endif]>
</object
Copy link to clipboard
Copied
Sup, I'm learning about this topic and I'm so interested in get more details about what you did here loading external mp3 files with flash vars.
I try the code you post here but is not working is not loading the mp3. Do you have any source code or can you give me more details please?
thanks in advance!
Copy link to clipboard
Copied
How did you do this in html?
Tnx
Find more inspiration, events, and resources on the new Adobe Community
Explore Now