I'm working on a very simple AS3 audio player. I only need it to play one mp3 file, so no need for playlists and such. Figured the easiest thing to do would be to call the mp3 filename from FlashVar in the HTML code, since the player will need to be used in a few separate places with different files each time.
Right - should have mentioned earlier that attempting to pull the string into a textfield returns nothing. I tried trace as well, no result.
Unfortunately, I'm unable to access the server logs easily - would involve countless requests and pleading to the development server admins. If this turns out to be an unavoidable step, I'll definitely do so.
Thanks for your quick response, by the way - this has been driving me nuts for days!
Try to avoid initializing variables outside a function with non-constant values. It's not wrong per se, but the order of execution is non-obvious and you get results like this.
Your flashvars and music declaration/initialization lines are occurring before the flashvars have been populated. To be safe, you can delay the code which gets flashvars until the instance is put on the stage.
Code:
package com.krankota{
import flash.display.*;
import flash.events.*;
import flash.media.*;
import flash.net.*;
import flash.geom.*;
public class AudioSource extends Sprite {
var sc:SoundChannel;
var isPlaying:Boolean = false;
var pos:Number = 0;
var dragging:Boolean = false;
var rectangle:Rectangle = new Rectangle(0,0,100,0);
var music:Sound;
public function AudioSource():void {
stop_btn.buttonMode = true;
stop_btn.addEventListener(MouseEvent.CLICK, stopMusic);
play_btn.buttonMode = true;
play_btn.addEventListener(MouseEvent.CLICK, playMusic);
pause_btn.buttonMode = true;
pause_btn.addEventListener(MouseEvent.CLICK, pauseMusic);
volume_mc.slider_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void{
stage.addEventListener(MouseEvent.MOUSE_UP, dropIt);
var flashvars:Object = loaderInfo.parameters;
music = new Sound(new URLRequest(flashvars.myVar));
}
private function stopMusic(e:Event):void {
if (sc != null) {
sc.stop();
pos = 0;
isPlaying = false;
}
}
private function playMusic(e:Event):void {
if (!isPlaying) {
sc = music.play(pos);
isPlaying = true;
}
}
private function pauseMusic(e:Event):void {
if (isPlaying) {
pos = sc.position;
sc.stop();
isPlaying = false;
}
}
private function dragIt(e:Event):void {
volume_mc.slider_mc.startDrag(false,rectangle);
dragging = true;
volume_mc.slider_mc.addEventListener(Event.ENTER_FRAME, adjustVolume);
}
private function dropIt(e:Event):void {
if (dragging) {
volume_mc.slider_mc.stopDrag();
dragging = false;
}
}
private function adjustVolume(e:Event):void {
var vol:Number = volume_mc.slider_mc.x / 100;
var st:SoundTransform = new SoundTransform(vol);
if (sc != null) {
sc.soundTransform = st;
}
}
}
}
See if this works a little better. Also, you were not using onComplete, so I removed it.
Definitely a huge step in the right direction. Previously, whenever I attempted to load the flashvars, everything stopped working - like, the swf would load, but I couldn't make anything work (easily tested by attempting to drag the volume slider).
Now, I still get no audio, but the volume slider works, so I guess the problem is sort of localized now to the flashvars just never loading.
I'm going to focus on that, see if I can't figure it out. Any help anyone can provide is immensely welcome.
And 5TonsOfFlax - I really appreciate your assistance and cleanup of my code. You're a spectacular resource!
Are you using the debug player? Use it. It should report any uncaught runtime errors for you. I suspect that something is throwing an error that you are not seeing.
Possibly a retarded question: given that I'm passing in the FlashVars from HTML, how would I use the debug player? Because isn't it just going to throw a URL must be non-null error without that flashvar?
Installing the debug player plugin should override the release player plugin. It's a bit annoying to switch back and forth, so you may want to set up separate browser instances (chrome with debug player, firefox with release, or whatever) or even a virtual machine for the purpose.
OK, so after a blissful weekend of not thinking about this problem at all, I'm back at it.
Using the debug player, I get the following error when I load the page from the dev server:
TypeError: Error #2007: Parameter url must be non-null.
at flash.media::Sound/_load()
at flash.media::Sound/load()
at flash.media::Sound()
at com.krankota::AudioSource/init()
Clicking around afterward, hitting the play button gives me this error, unsurprisingly:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.krankota::AudioSource/playMusic()
So obviously the file just isn't loading at all. I've tried moving the file around, I've tried putting in the full URL to the MP3 file on the server.
Any ideas? At this point, I'm considering just giving up and hard-coding the file in and just publishing a different SWF for each file I need to play throughout the project...UGH.
I don't know why you're having so much trouble getting the flashvars to work. I just built a very simple proof of concept to load a value from flashvars, that is very much like yours. Included are two different ways to embed it on the page (object/embed using your code, and swfobject).
I looked at the code you attached, and realized there was really only one difference: the HTML I had included the AC_RunActiveContent.js generated from publishing Flash HTML. For whatever reason, that totally nuked the whole thing. Once I removed that from my HTML code, everything works like a charm.
So, I assume the takeaway here is never let Flash publish your HTML, even when you're just testing.
Thanks SO much for you help. You're absolutely magnificent.