osmf sprint 8 example player

Posted on 07 January 2010 by nathan

I have started working with Adobe’s Open Source Media Framework. To get started I hunted down some examples to see how others dove into it – and found a few great ones, but most that relied on previous sprints. As you may or may not know, OSMF is still in prerelease which means that anything could change, and for traits that is exactly what happened between sprint 7 and sprint 8.

This change (along with some others) made many of the examples that i found not work – including a good one from @lisamarienyc on the adobe forums that I thought was best.

So I rewrote some portions and the resulting code is below:
questions, corrections, comments, and suggestions welcome.

package {

import flash.display.Sprite;
import org.osmf.events.LoadEvent;
import org.osmf.events.MediaError;
import org.osmf.events.MediaErrorEvent;
import org.osmf.events.MediaErrorCodes;
import org.osmf.events.MediaPlayerCapabilityChangeEvent;
import org.osmf.media.MediaPlayer;
import org.osmf.media.URLResource;
import org.osmf.net.NetLoader;
import org.osmf.traits.LoadState;
import org.osmf.traits.MediaTraitType;
import org.osmf.video.VideoElement;
import org.osmf.utils.URL;

public class OSMFExampleTwo extends Sprite {

	//our local flv to be played
    private const PROGRESSIVE:String = "trailer.flv";  //path to your progressive flv here

    private var _player:MediaPlayer;
	private var _video:VideoElement;

    public function OSMFExampleTwo() {
        trace("initialized");

		//create a new media player
		_player = new MediaPlayer();
        _video = new VideoElement(new NetLoader(), new URLResource(new URL(PROGRESSIVE)));

		//add an event listener to the video element to catch netstream failed errors
		_video.addEventListener(MediaErrorEvent.MEDIA_ERROR, onLoadEvent);

		//add an event listener so that when the player is capable of being viewed we can add it to the display list and watch
		_player.addEventListener( MediaPlayerCapabilityChangeEvent.VIEWABLE_CHANGE, onViewable );

		//add a new video element to the player
	   	_player.element = _video;

        }

	   //if the event fails - what comes through:

        private function onLoadEvent( e:MediaErrorEvent) :void {
           trace('ERROR (onLoadEvent): recieved a media error event');

		   var errorCode = e.error.errorCode;
		   	switch (errorCode) {
				case MediaErrorCodes.STREAM_NOT_FOUND:
					trace('ERROR (onLoadEvent): there was an error finding the file that was specified');
					break;
				case MediaErrorCodes.PLAY_FAILED:
					trace('ERROR (onLoadEvent): playback has failed');
					break;
				case MediaErrorCodes.NO_SUPPORTED_TRACK_FOUND:
					trace('ERROR (onLoadEvent): there was no supported track found');
					break;
				case MediaErrorCodes.FILE_STRUCTURE_INVALID:
					trace('ERROR (onLoadEvent): the file structure is invalid');
					break;
			}
        }

        private function onViewable( e:MediaPlayerCapabilityChangeEvent ) :void {
            if( e.enabled ) {
                addChild( _player.view );
                trace('player view');
            }
        }
    }
}

3 Comments For This Post

  1. Yujin Says:

    It will be good if some one publish a osmf flash player with basic controls like start, stop, pause and full screen. I think open video player got one such player.

  2. nathan Says:

    Yujin, i will see what i can do!

  3. evan Says:

    @Yujin, There is an example here http://blog.nicolasprof.com/profmediaplayer

    Made from sprint 0.8. There is also an example player with an external skin in the flex examples.

    hope that helps!

Leave a Reply

Photos from our Flickr stream

See all photos