0

Is it possible to use Actionscript 3 file reference from javascript. I need to code an application that will use javascript to call the browse method of the fileReference.

I wanted to be sure this is possible before I put too much time into the code. I did not know if there were some security issues there.

Gio
  • 1,954
  • 3
  • 22
  • 42
james
  • 2,595
  • 9
  • 43
  • 70
  • 1
    No you can not. FileReference.browse() needs to be called via user interaction with the flash app IE:a user click. However if this was an AIR app then you browse all you want without the need for user interaction. – The_asMan Sep 20 '12 at 13:16

1 Answers1

0

You can call anything you want defined in AS3 from Javascript. The thing is, you won't be able to directly use the FileReference object in your Javascript, but you can write all the methods you need in Actionscript, and then call them when needed. So AS3 would be something like a data provider.

Let me know if you need some code examples. If you're new to this check out ExternalInterface.

Edit:

So do you want to load the .mp3 from Flash side or from JS? If you want to use a FileReference I guess it's from Flash. Anyway, Flash will need to have the .mp3, as a ByteArray or as a link to it, so it can then load and play the sound.

Basically you need to make everything Flash sided. I mean the actual volume, play/pause and other functionality should be implemented on the Flash side, but controlled from Javascript.

You can add callbacks for methods in Flash, so that afterwards you can do something like this from javascript:

flashObject.playMusic();

On the Flash side you would need to write something like this:

if(ExternalInterface.available) {
    ExternalInterface.addCallback("playMusic", playMusic);
}

function playMusic():void {
    musicObject.play();
}

So what we do here is, add a callback to a Flash object, so that playMusic method can be called from Javascript, then define our actual method for playing the sound object. You can pass and get parameters like you'd do normally.

Keep in mind to set the allowScriptAccess parameter of the Flash object in HTML to always.

Gio
  • 1,954
  • 3
  • 22
  • 42
  • Well what I will be doing is using the javascript to load an mp3. Then play the MP3 with Flash, but use a javascript to control the volume, play, stop. Yeah some code might help. Thanks – james Sep 20 '12 at 10:01
  • I know how to use External Interface, just not sure about using the FileReference with the MP3. – james Sep 20 '12 at 10:05
  • You won't need to use a `FileReference` you can just pass URL-s to Flash and then load them using a simple loader. – Gio Sep 20 '12 at 10:14
  • Hey Gio. I need the user to select the file, so I cannot just use the simple loader. I am confused about the ByteArray bit. I do not understand why it needs to be a ByteArray. I know I need to do the play pause etc as Actionscript functions they call them from javascript. That is no problem, just the FileReference.. Especially now that you mentioned it must be in a ByteArray. – james Sep 20 '12 at 11:06
  • 1
    Flash player has security measures, that make it so FileReference, and FullScreen can only be invoked by user interaction originating in flash. – BadFeelingAboutThis Sep 20 '12 at 16:54
  • Just as @LondonDrugs_MediaServices said, if you actually want to use the FileReference class you're gonna need to make the whole player in Flash. Or just make the Browse button in Flash and then after choosing the mp3 file hide it from JS. Edit: When making a Browse button I mean that that SWF file will contain all the player methods too. – Gio Sep 20 '12 at 22:58