Odin's Blog

Odin's Blog

Using FlashVars to pass cue points array to Flash

Flash / AS 2.0Posted by Odin HG Sun, February 24, 2008 03:18:17
In this tutorial I'll use FlashVars to pass a array containing cue points to a MediaPlayback component into flash.

In this tutorial I'm going to use it to stream mp3 files.
You can of course use it with flv files too.

First, create a new flash document (550x400), open the components window and drag a MediaPlayback component and a button to the stage.
Label the button "Cue" or what ever you want, and give it a instance name of "btn1".
Blog Image
Now, give the MediaPlayback component a instance name of "player", and go to its parameters and set contentPath to a mp3 file
(ex. http://www.example.com/my_audio.mp3).
Also set mediaType to MP3, and controlPolicy to On.

Finished?
Let's select the first frame in timeline and open the actions panel.

First add:

stop();

var cue:Array = cuepoints.split(",");
current = 0;


Explanation:

stop();

Stop the movie.

var cue:Array = cuepoints.split(",");
Create a new array of the variable cuepoints.
The split function splits the variable at every comma.
This because our variable will look something like this:
"10,40,50,200,420".
The variable cuepoints is passed trough FlashVar, I'll get back to this later.


Add this code under the previous one:

_root.btn1.onRelease = function() {
_root.player.play(cue[current]);
current++;
if (current>=cue.length) {
current = 0;
}
};


Explanation:

_root.btn1.onRelease = function() {
This line checks if the button is pressed (or here, released).

_root.player.play(cue[current]);
Tell the player to play from the next cue point in our "cue" array.

current++;
Increase the current variable with 1.
So when we press the button again, it will play from the next cuepoint in our array.

if (current>=cue.length) {
current = 0;
}
If the current cue point is greater or equal to the number of total cue points, set the first cue point as next.

};
Close the onRelease function.

Full code:
stop();

var cue:Array = cuepoints.split(",");
current = 0;



_root.btn1.onRelease = function() {
_root.player.play(cue[current]);
current++;
if (current>=cue.length) {
current = 0;
}
};


Now, go to "File -> Export -> Export Movie".
Name it "player.swf" and click "Save".
When you are going to test this local on your computer,
you'll need to set "Local playback security" to "Access network only",
IF your mp3 file is on a webserver. Else you can just let it stay at
"Access local files only".


And now, the part where we're going to pass the flashvars from html.

Create a new HTML document in the same folder as your "player.swf".
And paste this in it:

<!--[if !IE]> -->
<object type="application/x-shockwave-flash" data="player.swf" width="550" height="400">
<!-- <![endif]-->
<!--[if IE]><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="550" height="400">
<param name="movie" value="player.swf" /><!-->
<!--dgx-->
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<param name="FlashVars" value="cuepoints=10,20,30,40,50,60,70" />
<p>You need<a href="http://www.macromedia.com/go/getflashplayer" target="_blank">Adobe FlashPlayer</a> installed to view this.</p>
</object>



This is just the regular way to put flash into html, but the important line here is:

<param name="FlashVars" value="cuepoints=10,20,30,40,50,60,70" />

This is where your cuepoints is stored.

The cuepoints are given in seconds, so when you press the button the first time, it will play from 10 seconds in this example, second time 20 seconds...

If you have questions or just want to express your feelings,
post a comment below.

Odin. smiley

  • Comments(6)

Fill in only if you are not real





The following XHTML tags are allowed: <b>, <br/>, <em>, <i>, <strong>, <u>. CSS styles and Javascript are not permitted.
Posted by cristian Fri, June 11, 2010 19:13:26

Its not working on html , why ? i put the <-If Ie ..... code in html ,please help ! Thx

Posted by tudor Fri, October 30, 2009 18:05:58

thanks

Posted by boverje Tue, January 13, 2009 06:33:26

is there a way to link embedded cue points in a .flv to an html doc? I want to build a menu outside of my flash document to access "chapters" in a flash video using links in a web page. I cannot find any reference to this and the only way I can find is to use flash actionscript and build my links inside of flash.

if you want to email me I'm at designerla@boverman.com

thanks - jb

Posted by djodin Sun, August 03, 2008 01:30:11

Take a look at http://weblogs.macromedia.com/flashjavascript/

Posted by neeraj8585@gmail.com Thu, March 27, 2008 09:28:41

plz mail me the code of above question in my email..thanks in advance

Posted by neeraj8585@gmail.com Thu, March 27, 2008 09:27:21

i hav a flv player,inside a html pagein html page one button is there that button should be invisible until user click & suppose it reach to a particular cue point after reaching particular cue point that html button should be automatically active any idea how to do this type of thing using javascript in flash plz provide the code for that