A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: [RESOLVED] FlashVars - I'm at a loss.

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Location
    Dallas, TX
    Posts
    10

    resolved [RESOLVED] FlashVars - I'm at a loss.

    Hi all.

    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.

    The HTML (relevant portion only):

    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="265" height="25" id="AudioPlayer" align="middle">
    	<param name="allowScriptAccess" value="sameDomain" />
    	<param name="allowFullScreen" value="false" />
    	<param name="movie" value="AudioPlayer.swf" />
        <param name="quality" value="high" />
        <param name="bgcolor" value="#ffffff" />
        <param name="FlashVars" value="myVar=052010.mp3">
        <embed src="AudioPlayer.swf" FlashVars="myVar=052010.mp3" quality="high" bgcolor="#ffffff" width="265" height="25" name="AudioPlayer" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
    	</object>
    And the ActionScript (again, just the relevant part):

    Actionscript Code:
    var flashvars:Object = loaderInfo.parameters;
    var myVar:String = flashvars.myVar;
    var music:Sound = new Sound(new URLRequest(myVar));

    And then the music variable is called. I've tested my player, and everything works fine when I define the music variable within the AS:
    Actionscript Code:
    var music:Sound = new Sound(new URLRequest("052010.mp3"));

    It's just when FlashVars are introduced that it blows up. It compiles with no error messages, just doesn't work.

    Any thoughts? I'm tearing my hair out here.

    Thanks!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Check to see whether the value is successfully retrieved from the flashvars. Put it in a textfield, or trace it, or something.

    Also check the http server logs to see what file is being requested.

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Location
    Dallas, TX
    Posts
    10
    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!

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    No, if the info never got out of the flashvars, then there's no way the correct request was being made. You don't need to worry about the logs yet.

    Is the code to pull the flashvars from the loaderinfo in the document class? if not, you may have better luck with:
    Code:
    var flashvars:Object = root.loaderInfo.parameters;
    var myVar:String = flashvars.myVar;
    var music:Sound = new Sound(new URLRequest(myVar));
    If the code you are trying to get the flashvars from is actually in a separately loaded swf, it's a little more involved.

  5. #5
    Junior Member
    Join Date
    Jun 2010
    Location
    Dallas, TX
    Posts
    10
    Yeah, the code is in a file called AudioSource.as, the document class for AudioPlayer.swf is com.krankota.AudioSource.

    Would it be helpful to give the full source code? I feel like I'm probably making a ridiculous error somewhere.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Couldn't hurt.

  7. #7
    Junior Member
    Join Date
    Jun 2010
    Location
    Dallas, TX
    Posts
    10
    Here's the full AudioSource.as:

    Actionscript 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 flashvars:Object = loaderInfo.parameters;
            var music:Sound = new Sound(new URLRequest(flashvars.myVar));
               
            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);
                stage.addEventListener(MouseEvent.MOUSE_UP, dropIt);
            }

            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;
                }
            }
            private function onLoadComplete(e:Event):void {
                trace('load complete');
            }
        }
    }

    I'm pretty much a newbie when it comes to AS3, so I'm sure my code is ugly as sin. Apologies in advance.

    The buttons and volume slider are already on-stage in the .fla file. If I replace
    Actionscript Code:
    var music:Sound = new Sound(new URLRequest(flashvars.myVar));
    with
    Actionscript Code:
    var music:Sound = new Sound(new URLRequest("052010.mp3"));
    everything works fine.

    Thanks again for all your help!

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  9. #9
    Junior Member
    Join Date
    Jun 2010
    Location
    Dallas, TX
    Posts
    10
    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!

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  11. #11
    Junior Member
    Join Date
    Jun 2010
    Location
    Dallas, TX
    Posts
    10
    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?

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  13. #13
    Junior Member
    Join Date
    Jun 2010
    Location
    Dallas, TX
    Posts
    10
    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.

    Thanks again for all the assistance.

  14. #14
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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 hope this helps a little.
    Attached Files Attached Files

  15. #15
    Junior Member
    Join Date
    Jun 2010
    Location
    Dallas, TX
    Posts
    10
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center