Flash
  Home arrow Flash arrow Page 4 - Making an MP3/FLV Player with Flash MX 200...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Making an MP3/FLV Player with Flash MX 2004 and JavaScript
By: S.M.H. Qadri
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 142
    2004-11-29

    Table of Contents:
  • Making an MP3/FLV Player with Flash MX 2004 and JavaScript
  • Designing an application interface with Flash MX 2004
  • Adding Action script to our Flash interface
  • Writing the JavaScripts

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Making an MP3/FLV Player with Flash MX 2004 and JavaScript - Writing the JavaScripts


    (Page 4 of 4 )

    Open your favorite text editor (mine is Dreamweaver MX 2004, but notepad will also work). In this part we will make two Web pages. One will hold our flash movie, and the other one will be used for selecting the MP3/FLV songs. So let’s create them.

    Write the following codes in your text editor and save the file as “Player.html” in the same folder where you saved your flash movie (MyMP3/FLVPlayer.swf):

    <html>
    <head>
    <title>Welcome To My MP3/FLV Player</title>
    </head>
    <body>
    <script language="JavaScript" >
    <!--
    function MM_openBrWindow(theURL,winName,features) { //v2.0
      window.open(theURL,winName,features);
    }
    -->
    </script>
    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/
    shockwave/cabs/flash/swflash.cab#version=7,0,0,0" name="movie" width="300" height="400" align="left" id="movie">
      <param name="allowScriptAccess" value="sameDomain" >
      <param name="movie" value="MyMP3Player.swf" >
      <param name="menu" value="false" >
      <param name="quality" value="high" >
      <param name="scale" value="noborder" >
      <param name="salign" value="lt" >
      <param name="wmode" value="transparent" >
      <param name="bgcolor" value="#000000" >
      <embed src="MyMP3Player.swf" menu="false" quality="high" scale="noborder" salign="lt" wmode="transparent" bgcolor="#000000" width="300" height="400" swliveconnect=true id="movie" name="movie" align="left" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="
    http://www.macromedia.com/go/getflashplayer" >
    </object>
    <br>
    </body>
    </html>

    Save this file as Player.html.

    This is just a simple html page that contains some conventional HTML tags like <html>, <head>, and <body>. There is another tag in these codes called the <object> tag. This code holds your flash movie. The information which is written in the <object> tag is also written in another tag nested in the <object> tag, called the <embed> tag. Why are we using both <object> and <embed> tags, since both of them have the same properties? Actually, we are using them because of the clash between Microsoft Internet Explorer and Netscape Navigator. The <object> tag is supported in Internet Explorer and the <embed> tag is supported in Netscape Navigator--but not in Internet Explorer. In this way, our player would be accessible from both Internet Explorer and Netscape Navigator (now Mozilla).

    Along with the conventional HTML tags, there is a code block of java script present that is:

    <script language="JavaScript">
    <!--
    function MM_openBrWindow(theURL,winName,features) { //v2.0
      window.open(theURL,winName,features);
    }
    -->
    </script>

    This block is here because, working on the Flash side, we dragged a button instance on stage and added some codes to that which were something like getURL(bla blab );la ...  That code was added to trigger this JavaScript. This JavaScript launches another page called “url.html”, an HTML file that we will use to browse our directories in search of MP3/FLV files. When you have saved the first file as Player.html, create a new file in your text editor and type following codes in it:

    <html>
    <head>
    <title>Select MP3/FLV Files Only Please</title>
    <script language="javascript">
    function fill(file){
    window.opener.movie.SetVariable("fName", file);
    window.close();
    }
    </script>
    </head>

    <body>
    <form id="form1" method="post" enctype="multipart/form-data" name="form1">
      Select an MP3/FLV &amp; Add to playlist. <br>
      <input name="file" type="file" accept="audio/basic">
    <br>
    <input type="submit" onClick="fill(file.value)" value="Add to list...">
    </form>
    </body>
    </html>

    Save this file as “url.html” where you saved the Player.html and MyMP3/FLVPlayer.swf.

    Now I will describe how this page works. Let’s start from the <body> tag. The <body> tag contains a simple form in which we have placed a file browsing field named “file” and a Submit button. By clicking on the “browse” button of the file field we will select our desired MP3/FLV File(we can select other files as well, but don’t select any file other then MP3/FLV Audios). When someone clicks on the Submit button, it will execute a JavaScript function called “fill()” that is written in the head area of our URL.HTML file. While executing that function, we are also sending some information to that function. See the “onClick” event in the Submit button’s coding where we are executing the function as:

    onClick=”fill(file.value)”

    where onClick is the event. When some one clicks on the button (submits the form), a function called fill(file.value) is triggered where “file.value” is the text (path to selected file) written in the file browsing field and fill is the name of the function which we are triggering.

    Now I will explain how the “fill” function works. It receives  the value from the file field and sets the value of the “fName” variable of the flash movie which is present in our main page (i.e. Player.html). Here in the line window.opener.movie.SetVariable("fName", file);, window.opener means our main page, which was the responsible for opening this new window.

    Here you have noticed that we are setting the value of a variable which is present in our Flash movie by using JavaScript. For this we are using the movie.SetVariable method. The actual syntax of this method is:

    Name_of_Movie.SetVariable(“name of variable in flash movie”);

    Where "name of variable in Flash movie" is the value we want to set using the JavaScript script.

    NOTE: Here you should remember that name of movie means the name that we given to our movie in <object> or <embed> tag, for example id=”movie” or name=”movie” not src=”MyMP3Player.swf”

    Now preview Player.html in your browser and check it. If everything is going fine then you have followed the tutorial perfectly. If not, you have missed something or done something wrong, so check your codes in both the pages and in your Flash movie; also check that you have given the same name as we mentioned in this tutorial because both Action script and JavaScript are case sensitive, so in both scripts “FNAME”, “fNAME”, “Fname” and “fName” are four different things.
     
    Conclusion:

    In this tutorial, we learned a little bit about how to talk with JavaScript using Flash MX 2004 and vice versa. There is another method called “fscommand” in flash by which we can talk to JavaScript or VB. There are some limitations in this tutorial, such as that the user can select any file other then MP3/FLV file, which will cause flash movie to stop working. This can be solved by using advanced validations of JavaScript or Flash’s Action Script.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · userOne... I'm logged in in one window but can't logIn to this one very...
       · Hey i did everything your tutorial says and it doesnt work. I tried it twice from...
       · May be you are using a browser other then Internet Explorer as the javascript...
       · while following this tutorial i realized the following code is incorrect "... on...
       · I am really amazed hearing that the codes are not working because i had about 70-75...
       · Well the player works fine but I am curious if I can have mp3 files already existing...
       · For this functionality, you have to use XML or shared objets, for this, check out...
       · GreetingsI faced the same problem. There are four errors when i export the flash...
       · these errors are just the result of an innappropriate line break in the geturl...
       · No dear! as far as only flash is concerned, you cant play with directories & files...
       · I had the same errors as you did and i still can't fix this thing???
       · Hi there! you guys are facing problems for nothing, actually, you are getting...
       · **Error** Scene=Scene 1, layer=controls, frame=1:Line 1: Invalid mouse event...
       · May be you are using Flash MX & the tutorials is written for Flash MX 2004,...
       · you mentioned that the javascript differs from IE to Firefox. any suggestions or...
       · Great tutorial, thanks alot! For those still having trouble making this stuff work...
       · Does your player actually play the mp3 file or just load it into the player list....
       · Ok, I've just got flash, and being a stupid newbie means i need help!!! when I do...
       · The Answer of this question is already given above.Syed Mazhar HasanAuthor
       · Will this tutorial work if using Flash 8?
       · if you are opening the player in your browser using physical paths...
       · Hi,I encountered the same problem. The coding is right but when you cut and paste...
       · Hi, I encountered the same errors. The solution I foune is to put the code you...
       · Hi, Great Tutorial! I am just wondering if it would be possible to add some code...
       · The mp3 loads into the list but doesn't play. Do you have to set the properties of...
       · hello…..can anyone help me enter data into mysql databasefrom a flash form...
       · When will you publish your article about how to use XML so you can have mp3 files...
       · Sorry, i m ASP/PHP/ASP.NET programmer and i dont know coldfusion at epert level, so...
       · Try using Document. instead of window.
       · I got "the documents contain no data" after clicking on the add to list.
       · You must be using Firefox or a non-Microsoft Internet explorer browser, the...
       · Great page, not too fancy but well balanced! Cheers!
       · Everything works, but I can't load a mp3 and actually listen to it. I can't add a...
       · You must be using a not-internet explorer browser, as the javascrip provided in this...
       · Hi!You must be using a non-internet explorer browser such as firefox, netscpe or...
       · Try this site. Do not bother to create swf.. These guys create everything. You just...
       · how do u make the playlist only show the song name not 'C:\Documents And...
       · Hey, I went to that site you mentioned but can't seem to get an answer. What do I...
       · The code didnt work for me either... Until I realized that when you paste it from...
       · Try putting the entire function call to getURL on one line rather than breaking it...
       · Hi. Yes you can get this.In action script: replace the line: ...
       · To make it working in FireFox and Opera...
       · This code modification does not make it work on firefox. It does however still work...
       · I think i did all that using marcomedia 8 spanish version...FirstIm getting some...
       · Really i appreciate this tutorial, how simple and elaborative style from SMH Qadri....
       · Really i appreciate this tutorial, how simple and elaborative style from SMH Qadri....
       · Thanks a lot.Syed Mazhar Hasan QadriAuthor
       · nice tutorial, but I have same problem as a guy some time ago. I open the...
       · It doesnt works with firefox, it was IE only as when i wrote that article, FF was in...
     

    FLASH ARTICLES

    - Decorator Pattern
    - Organizing Frames and Layers for Flash Anima...
    - Organizing Frames and Layers
    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications
    - ActionScript in Flex Applications
    - A Closer Look at Apollo`s File System API
    - Using the File System API
    - ActionScript 101
    - Flash Buttons
    - Advanced Flash Animation
    - Creating Your First Animated Movie with Flas...
    - Flash: Building Blocks






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT