The Simplest Possible OSMF Player

April 14, 2010 in Technology

I'm continuing work on my FITC Toronto talk, 'Standardize Your Flash with Adobe OSMF', and the files to accompany it.

OSMF certainly has features and functionality for hard-core developers to build powerful media-rich experiences. But OSMF also makes basic media player code really easy for those users of Flash who might not consider themselves developers, but who still are comfortable coding on the timeline.

One of the issues with the still nascent, but growing at an impressive rate OSMF documentation, is that almost all the sample code is for users of Flex Builder / Flash Builder -- and all of the sample code for Flash assumes use of external class files.

But using OSMF does not require coding in external class files. So, to illustrate that point, I've gone ahead and modified the simplest possible OSMF video player from Adobe's documentation to code that works on the timeline. Which I'm posting here.

So this is code (OSMF Sprint 10 v0.93) you can paste onto any timeline that will play video with OSMF.

Want to make it stream the video instead of playing it progressively? Point it to an RTMP URL instead, and it will automatically play.

  1. import org.osmf.containers.MediaContainer;
  2. import org.osmf.elements.VideoElement;
  3. import org.osmf.media.MediaPlayer;
  4. import org.osmf.media.URLResource;
  5. // Create the container class that displays the media.
  6. var container:MediaContainer = new MediaContainer();
  7. //add the MediaContainer instance to the stage
  8. addChild(container);
  9. // Create the resource to play and point it to the FLV
  10. var resource:URLResource=new URLResource("my.flv");
  11. // Create the MediaElement
  12. var videoElement:VideoElement=new VideoElement(resource);
  13. //add the VideoElement to our container class
  14. container.addMediaElement(videoElement);
  15. //create the MediaPlayer instance
  16. var mediaPlayer:MediaPlayer = new MediaPlayer();
  17. // Set the MediaElement on a MediaPlayer.
  18. //Because autoPlay defaults to true, playback begins immediately.
  19. mediaPlayer.media=videoElement;

Share and enjoy!

-r