Peter's Blog

Redefining the Impossible

Embedding Flash Player


Much as I hate flash, it is a nice way to embed a sound or video player in a page. Here's how I did it in rails using the JW FLV Media Player. I added the following to my view:

   1  <script type="text/javascript" src="/javascripts/swfobject.js"></script>
   2  
   3  <% for oRssItem in @rssitems %>
   4    <hr/>
   5    <h3><%= oRssItem.title %></h3>
   6    <p><%= oRssItem.description %></p>
   7    <table width="470">
   8      <tr>
   9         <td wIdth="50%"><%= oRssItem.created.strftime( "%m %B %y") %></td>
  10         <td align="right"><%= link_to "Download", "/#{ oRssItem.filename}" %></td>
  11      </tr>
  12    </table>
  13  
  14    <div id="player<%=oRssItem.id%>">This text will be replaced</div>
  15  
  16    <script type="text/javascript">
  17      var so = new SWFObject('/mediaplayer.swf','mpl','470','20','8');
  18      so.addParam('allowscriptaccess','always');
  19      so.addParam('allowfullscreen','true');
  20      so.addVariable('width','470');
  21      so.addVariable('height','20');
  22      so.addVariable('file','/<%= oRssItem.filename%>');
  23      so.write('player<%=oRssItem.id%>');
  24    </script>
  25  <% end %>

This generates a page full of podcast entries, each with a little media player thing so you can listen to them easily. This does mp3 files but it can handle video too.

I am aware that the swfobject.js would be better loaded in the html header.

Here's a demo: I don't know if this will work in the rss feed (UPDATE: no!) so bear with me and please be gentle on my bandwidth, it's quite a catchy tune:

Oops, no flash? Javascript broken? Works for me.

The main reason I am blogging this is because it only needs a change to a view file and installation of two files in the /public directory (the player and a javascript file). Since view files are awkward to comment legibly I decided to document it all here so I can find it again.


Filed under: mp3 rails ruby

Paul Says:

Thanks for your post on embedding JW player. I can't seem to get it to work for my videos although I got another player to work for the same content. I just prefer JW's over the player I was using since 1. jw is free 2. the other player only plays and can't pause, rewind, or anything unless you pay.

I realize this would be a bit more work, but do you think you could do a video tutorial on how you did it.

Any help is appreciated.

Thanks

Peter Says:

Right now I haven't tried video. Sometime soon someone will be giving me a file that they want put online so I will be dipping my toe in the water.

The only problem I anticipate is in converting a .avi file into a .flv file for which I intend to use ffmpeg. When I have done it I will blog it.

The JW site has a good Setup Wizard that I used to get the fundamentals of what I put here (I just the changed path names as you can see above).

One thing I see mentioned a lot is that the file names of the files you load into the media player should be given relative to the URL that the media player is in, not the html file that displays the media player. I avoided confusion by putting the media player in my html root.

Peter

Have Your Say

I welcome constructive comments or questions but I reserve the right to delete any comments that displease me.

Who are you?

(Optional) If you enter an email address here I might email you back. Your email address will not be sold to spammers or shown anywhere

What do you have to say?