SWF src= file.php

i'm making a flash music player for myspace.com
users will visit my site and create an account for uploading their own music file.
they will be given <embed> code to put into their page based on a primary key that is assigned in mysql when their account is setup. so it will read simiar to:
<embed src="http://www.somedomain.com/flashplayer.php?id=3">

once that php file is src'd by the player, the php will search my database based on the "id" passed and then set the actual SWF source for the flash player somewhere in the php header.

the problem is, i don't know how to do this exactly.

can i just use regular $_REQUEST["id"] to get the id? second... how do i set up the header information so that the "file.swf?someattribute=somevalue" src is what the <embed> src reads?

i'm pretty sure this is possible and not to difficult, but i don't know the code to accomplish it...

thanks,
jeremy
LVL 4
Jeremy DaleySenior DeveloperAsked:
Who is Participating?
 
Rob_JeffreyIT/ProgrammingCommented:
Okay - pass out a similar object tag to your users as below:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="400" id="flashplayer" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="flashplayer.php?id=3" />
<param name="FlashVars" value="id=3">
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="flashplayer.php?id=3" FlashVars="id=3" quality="high" bgcolor="#ffffff" width="550" height="400" name="flashplayer" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

Now you can use the FlashVars in actionscript to set the unique id from the user - use the lines param name="FlashVars" and in the embed source - point to the same variable name.

Now you can use a LoadVars call from flash to pull data from another PHP page - passing the user's id to the second page.

var my_lv:LoadVars = new LoadVars();
my_lv.onLoad = function(success:Boolean):Void {
    if (success) {
        trace(this.fileName);
    } else {
        trace("Error");
    }
}
my_lv.load("http://www.site.com/flashparams.php?id=" + id);


Hope this makes sense - it's untested but I can try to walk you through any problems.
0
 
Rob_JeffreyIT/ProgrammingCommented:
I havn't tested this - my FTP server just went down.
I use this code with PDFs and it works fine.  I've changed the content-type to be the proper for SWF files - so it should work.

The only thing to keep in mind with this is to use it with small files only.  This reads the entire file into memory before it sends it out.  

<?php
      $filename = 'test.swf';
      $fle = file_get_contents($filename);
      $len = filesize($filename);
              header('Content-type: application/x-shockwave-flash');
            header('Content-Length: ' . $len);
            header('Content-Disposition: inline; filename=' . $flashname);
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
      echo $fle;
?>
0
 
Jeremy DaleySenior DeveloperAuthor Commented:
okay, let me try that when i get off work... i'll let ya know the outcome

thanks
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Rob_JeffreyIT/ProgrammingCommented:
No problem.  I was able to finally test it - it worked fine for me here.
- just to let you know.
0
 
Jeremy DaleySenior DeveloperAuthor Commented:
okay, here's the situation:

it seems to work fine when all i'm doing is using a raw file (i.e. "somefile.swf")

but what i have to do is pass variables to the flash player in the file string. for example, a user can specify what color they want the player to be. i put that value in the database when they're setting their preferences for the player's attributes.

when the php file is called, it checks the database and passes that color value in the file extension like so: "somefile.swf?playerColor=red"

when i add that variable to the end, it doesn't work at all.

i don't know of any other way to get those variables to the flash player. things like artist and song title are also passed in that file string.
0
 
Rob_JeffreyIT/ProgrammingCommented:
You won't be able to pass command line info to the player with the method above.
Are you handing out code snips to people so they can add your flash file to their site?
Or are you trying to personalize logins for individual users on your site only?
Basically - do you have control over the object embed tag for the flash file being called?
0
 
Rob_JeffreyIT/ProgrammingCommented:
Sorry - just re-read your question above.  You are handing out the object tag to users...
I have a solution - but you will need .fla source control over the flash file being used.
Can you edit the flash file?
0
 
Jeremy DaleySenior DeveloperAuthor Commented:
yes, i have the source file.
0
 
Jeremy DaleySenior DeveloperAuthor Commented:
okay... i'll try it after work... i may need clarification once i get it started.

thanks
0
 
Jeremy DaleySenior DeveloperAuthor Commented:
will the flashparams just be plain text... or how does that work in flash?
0
 
Rob_JeffreyIT/ProgrammingCommented:
Just like any variable - actionscript changes the type based on content.  If you try to add a variable to another like a + b they will either be treated as numbers or strings - depending on what's in them:
a=1;
b=2;
(a+b = 3)

a="d";
b="f";
(a+b="df")

Etc.  So if you need to pass string - it works fine as well.  If you want to be able to pass a filename to a user then you probably want to have control to change that yourself.  If you simply told them to use an embed command on their page that had the file name embedded - then you won't be able to change it without getting them to edit their web page for each file.
0
 
Jeremy DaleySenior DeveloperAuthor Commented:
instead of using a php src... couldn't i just use the swf file with:
src="flashplayer.swf?id=3"

and then in the actionscript, send the same:
my_lv.load("http://www.site.com/flashparams.php?id=" + id);
command?

... do my database lookup from that php? turn it into a text file from "header" commands?
0
 
Rob_JeffreyIT/ProgrammingCommented:
... uh .... yes....

;)
0
 
Rob_JeffreyIT/ProgrammingCommented:
Yeah.  The only thing I can see as a benefit from generating a dynamic swf would be that the users will never know where the actual swf file is located on your site.  But - they would still be able to grab the file from their cache and host it directly.  So - there is no real benefit.

Good call on that - completely missed the obvious :)
0
 
Jeremy DaleySenior DeveloperAuthor Commented:
well, i ended up using the sendAndLoad command instead. it was pretty easy for me to just:

onClipEvent (load) {
      $envelope = new LoadVars();
      $envelope_received = new LoadVars();
      $id = _root.id;
      $envelope.id = $id;
      $envelope_received.onLoad=function(){
                              //stuff to do when vars are received back from php file
                             var someVar = $envelope_received.var1;
      }
      $envelope.sendAndLoad("myspace_player.php", $envelope_received,"GET");
}

my php file just used:

$id=$_GET["id"];

i did some mysql calls and then just echoed out a string of vars attached in one line:

echo "var1=value1&var2=value2";

the flash just picks up the echo

your method pretty much got me on the track i needed. i looked into the actionscript dictionary a bit more and picked up that method instead cause it seemed a little easier for me.
0
 
Rob_JeffreyIT/ProgrammingCommented:
Definately.
Most people don't have access to the source of the flash files they use - I completely missed the obvious.
I'm glad I could help even if it was a round about -way.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.