본문 바로가기

AS3/ActionScript

osmf 샘플

Open Source Media Framework (OSMF) simplifies the development of media players by allowing developers to assemble components to create high-quality, full-featured video playback experiences.
This open framework enables development focused on web-based video monetization, with lower costs and faster turnaround.
It ‘s ActionScript3.0 based and was created by Adobe.
OSMF is designed for developers Adobe Flash / Flex / Air.

I created some examples to work with OSMF, including creating a simple video player, an advanced player, load external SWF files and external images, play audio and mp3 files.

SIMPLE VIDEO PLAYER

package com.flepstudio
{
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;

    import org.osmf.media.MediaPlayerSprite;
    import org.osmf.media.URLResource; 

    public class Main extends MovieClip
    {
        private const VIDEO_URL:String="http://flepstudio.org/utilita/VideoPlayer/IronMan2.mov";

        private var url_resource:URLResource;

        private var video_sprite:MediaPlayerSprite;

        public function Main()
        {
            addEventListener(Event.ADDED_TO_STAGE,init);
            addEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }

        private function init(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);

            this.video_sprite=new MediaPlayerSprite();
            url_resource=new URLResource(VIDEO_URL);
            this.video_sprite.resource=url_resource;

            this.addChild(video_sprite);
        }

        private function destroy(evt:Event):void
        {
            removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }
    }
}

SIMPLE VIDEO PLAYER WITH PLAY BUTTON

package com.flepstudio
{
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;

    import org.osmf.media.MediaPlayerSprite;
    import org.osmf.media.URLResource;
    import org.osmf.events.PlayEvent;

    public class Main extends MovieClip
    {
        private const VIDEO_URL:String="http://flepstudio.org/utilita/VideoPlayer/IronMan2.mov";

        private var url_resource:URLResource;

        private var video_sprite:MediaPlayerSprite;

        private var play_button:PlayButton;

        public function Main()
        {
            addEventListener(Event.ADDED_TO_STAGE,init);
            addEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }

        private function init(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);

            createOSMFvideo();
            createPlayButton();
            addListeners();
        }

        private function createOSMFvideo():void
        {
            video_sprite=new MediaPlayerSprite();
            video_sprite.mediaPlayer.autoPlay=false;
            url_resource=new URLResource(VIDEO_URL);
            video_sprite.resource=url_resource;

            addChild(video_sprite);
        }

        private function createPlayButton():void
        {
            play_button=new PlayButton();
            play_button.x=stage.stageWidth/2-play_button.width/2;
            play_button.y=stage.stageHeight/2-play_button.height/2;
            addChild(play_button);
        }

        private function addListeners():void
        {
            play_button.mouseChildren=false;
            play_button.buttonMode=true;
            play_button.addEventListener(MouseEvent.CLICK, playVideo);

            video_sprite.mediaPlayer.addEventListener(PlayEvent.PLAY_STATE_CHANGE,checkState);
        }

        private function playVideo(evt:MouseEvent):void
        {
            video_sprite.mediaPlayer.play();
        }

        private function checkState(evt:PlayEvent):void
        {
            play_button.visible=!video_sprite.mediaPlayer.playing;
        }

        private function destroy(evt:Event):void
        {
            removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }
    }
}

SIMPLE VIDEO ELEMENT

package com.flepstudio
{
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;

    import org.osmf.containers.MediaContainer;
    import org.osmf.elements.VideoElement;
    import org.osmf.events.*;
    import org.osmf.media.*;
    import org.osmf.net.*;

    public class Main extends Sprite
    {
        private const PROGRESSIVE:String="http://flepstudio.org/utilita/VideoPlayer/IronMan2.mov";
        private var url_resource:URLResource;
        private var media_container:MediaContainer;
        private var media_player:MediaPlayer;
        private var net_loader:NetLoader;
        private var video_element:VideoElement;

        public function Main()
        {
            addEventListener(Event.ADDED_TO_STAGE,init);
            addEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }

        private function init(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);

            createOSMFvideo();
        }

        private function createOSMFvideo():void
        {
            net_loader=new NetLoader();

            video_element=new VideoElement();

            url_resource=new URLResource(PROGRESSIVE);
            video_element.resource=url_resource;

            media_player=new MediaPlayer();
            media_player.bufferTime=5;
            media_player.addEventListener(BufferEvent.BUFFERING_CHANGE,onBufferUpdated);
            media_player.addEventListener(TimeEvent.CURRENT_TIME_CHANGE,onTimeUpdated);
            media_player.addEventListener(LoadEvent.BYTES_LOADED_CHANGE,onBytesUpdated);
            media_player.media=video_element;

            media_container=new MediaContainer();
            media_container.addMediaElement(video_element);

            addChild(media_container);
            swapChildren(media_container,info_mc);
        }

        private function onBufferUpdated(evt:BufferEvent):void
        {
            info_mc.buffering_txt.appendText('buffering: '+evt.target.buffering+'\n'+ 'buffer length: '+evt.target.bufferLength+'\n');
        }

        private function onTimeUpdated(evt:TimeEvent):void
        {
            info_mc.time_txt.text='\n'+'time: '+int(evt.target.currentTime)+'\n'+'duration: '+int(evt.target.duration)+'\n';
        }

        private function onBytesUpdated(evt:LoadEvent):void
        {
            info_mc.bytes_txt.text='\n'+'bytes loaded: '+evt.target.bytesLoaded+'\n'+'bytes total: '+evt.target.bytesTotal+'\n'+'percentage: '+Math.floor((evt.target.bytesLoaded/evt.target.bytesTotal)*100)+' %';
        }

        private function destroy(evt:Event):void
        {
            removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }
    }
}

ADVANCED VIDEO ELEMENT

package com.flepstudio
{
    import flash.display.*;
    import flash.text.*;
    import flash.geom.*;
    import flash.events.*;

    import org.osmf.containers.MediaContainer;
    import org.osmf.elements.VideoElement;
    import org.osmf.events.*;
    import org.osmf.media.*;
    import org.osmf.net.*;

    public class Main extends Sprite
    {
        //private const PROGRESSIVE:String="http://www.flepstudio.org/utilita/VideoPlayer/IronMan2.mov";
        private const PROGRESSIVE:String="http://php:8888/IronMan2.mov";
        private var url_resource:URLResource;
        private var media_container:MediaContainer;
        private var media_player:MediaPlayer;
        private var net_loader:NetLoader;
        private var video_element:VideoElement;

        public function Main()
        {
            addEventListener(Event.ADDED_TO_STAGE,init);
            addEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }

        private function init(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);

            console_mc.pause_mc.gotoAndStop(1);
            console_mc.play_mc.gotoAndStop(1);
            console_mc.stop_mc.gotoAndStop(1);

            createOSMFvideo();
            addListeners();
        }

        private function createOSMFvideo():void
        {
            net_loader=new NetLoader();

            video_element=new VideoElement();

            url_resource=new URLResource(PROGRESSIVE);
            video_element.resource=url_resource;

            media_player=new MediaPlayer();
            media_player.bufferTime=5;
            media_player.autoPlay=true;
            media_player.addEventListener(BufferEvent.BUFFERING_CHANGE,onBufferUpdated);
            media_player.addEventListener(TimeEvent.CURRENT_TIME_CHANGE,onTimeUpdated);
            media_player.addEventListener(LoadEvent.BYTES_LOADED_CHANGE,onBytesUpdated);
            media_player.addEventListener(PlayEvent.PLAY_STATE_CHANGE,checkPlayState);
            media_player.media=video_element;

            media_container=new MediaContainer();
            media_container.addMediaElement(video_element);

            addChild(media_container);
            swapChildren(media_container,console_mc);
        }

        private function addListeners():void
        {
            console_mc.pause_mc.mouseChildren=false;
            console_mc.pause_mc.buttonMode=true;
            console_mc.pause_mc.addEventListener(MouseEvent.MOUSE_DOWN,onConsoleDown);

            console_mc.play_mc.mouseChildren=false;
            console_mc.play_mc.buttonMode=true;
            console_mc.play_mc.addEventListener(MouseEvent.MOUSE_DOWN,onConsoleDown);

            console_mc.stop_mc.mouseChildren=false;
            console_mc.stop_mc.buttonMode=true;
            console_mc.stop_mc.addEventListener(MouseEvent.MOUSE_DOWN,onConsoleDown);

            console_mc.volumebar_mc.knob_mc.x=console_mc.volumebar_mc.bar_mc.x+console_mc.volumebar_mc.bar_mc.width-console_mc.volumebar_mc.knob_mc.width+2;
            console_mc.volumebar_mc.knob_mc.mouseChildren=false;
            console_mc.volumebar_mc.knob_mc.buttonMode=true;
            console_mc.volumebar_mc.knob_mc.addEventListener(MouseEvent.MOUSE_DOWN,onKnobDown);
        }

        private function onConsoleDown(evt:MouseEvent):void
        {
            switch(evt.target.name)
            {
                case "pause_mc":
                    if(evt.target.currentFrame==1)
                    {
                        media_player.pause();
                        evt.target.gotoAndStop(2);
                    }
                    else
                    {
                        media_player.play();
                        evt.target.gotoAndStop(1);
                    }
                break;

                case "play_mc":
                    disablePlayButton();
                    enableStopButton();
                    media_player.play();
                break;

                case "stop_mc":
                    disableStopButton();
                    enablePlayButton();
                    console_mc.pause_mc.gotoAndStop(1);
                    media_player.stop();
                break;
            }
        }

        private function onKnobDown(evt:MouseEvent):void
        {
            var rect:Rectangle=new Rectangle(console_mc.volumebar_mc.bar_mc.x+console_mc.volumebar_mc.bar_mc.width-evt.target.width+2,
                                             evt.target.y,-console_mc.volumebar_mc.bar_mc.width+evt.target.width-4,0);
            evt.target.startDrag(false,rect);
            evt.target.addEventListener(Event.ENTER_FRAME,volumeControl);
        }

        private function volumeControl(evt:Event):void
        {
            var distance:Number=console_mc.volumebar_mc.bar_mc.width-evt.target.width;
            var p:Number=evt.target.x-console_mc.volumebar_mc.bar_mc.x;
            var percentage:Number=Math.ceil((p/distance)*100);
            media_player.volume=percentage/100;
        }

        private function checkPlayState(evt:PlayEvent):void
        {
            if(evt.target.playing)
            {
                enablePauseButton();
                disablePlayButton();
            }
            else
            {
                disablePauseButton();
                enablePlayButton();
            }
        }

        private function enablePauseButton():void
        {
            console_mc.pause_mc.gotoAndStop(1);
            console_mc.pause_mc.mouseEnabled=true;
        }

        private function disablePauseButton():void
        {
            console_mc.pause_mc.gotoAndStop(2);
            console_mc.pause_mc.mouseEnabled=false;
        }

        private function enablePlayButton():void
        {
            console_mc.play_mc.gotoAndStop(1);
            console_mc.play_mc.mouseEnabled=true;
        }

        private function disablePlayButton():void
        {
            console_mc.play_mc.gotoAndStop(2);
            console_mc.play_mc.mouseEnabled=false;
        }

        private function enableStopButton():void
        {
            console_mc.stop_mc.gotoAndStop(1);
            console_mc.stop_mc.mouseEnabled=true;
        }

        private function disableStopButton():void
        {
            console_mc.stop_mc.gotoAndStop(2);
            console_mc.stop_mc.mouseEnabled=false;
        }

        private function onBufferUpdated(evt:BufferEvent):void
        {
            //trace('buffering: '+evt.target.buffering+' buffer length: '+evt.target.bufferLength);
        }

        private function onTimeUpdated(evt:TimeEvent):void
        {
            //trace('time: '+int(evt.target.currentTime)+'\n'+'duration: '+int(evt.target.duration));
        }

        private function onBytesUpdated(evt:LoadEvent):void
        {
            //trace('buffering');
            //trace('bytes loaded: '+evt.target.bytesLoaded+'\n'+'bytes total: '+evt.target.bytesTotal+'\n'+'percentage: '+Math.floor((evt.target.bytesLoaded/evt.target.bytesTotal)*100)+' %');
        }

        private function destroy(evt:Event):void
        {
            removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }
    }
}

AUDIO ELEMENT

package com.flepstudio
{
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;

    import org.osmf.containers.MediaContainer;
    import org.osmf.elements.AudioElement;
    import org.osmf.events.TimeEvent;
    import org.osmf.media.MediaPlayer;
    import org.osmf.media.URLResource;

    public class Main extends Sprite
    {
        private const TRACK_URL:String="http://www.flepstudio.org/music/dire_straits/13 - Sultans of Swing.mp3";

        private var url_resource:URLResource;

        private var media_player:MediaPlayer;

        private var audio_element:AudioElement;

        public function Main()
        {
            addEventListener(Event.ADDED_TO_STAGE,init);
            addEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }

        private function init(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);

            createOSMFaudio();
        }

        private function createOSMFaudio():void
        {
            media_player=new MediaPlayer();
            url_resource=new URLResource(TRACK_URL);
            audio_element=new AudioElement();
            audio_element.resource=url_resource;

            media_player.volume=.5;
            media_player.loop=true;
            media_player.addEventListener(TimeEvent.CURRENT_TIME_CHANGE,onTimeUpdated);
            media_player.addEventListener(TimeEvent.DURATION_CHANGE,onTimeUpdated);
            media_player.autoPlay=true;
            media_player.media=audio_element;
        }

        private function onTimeUpdated(evt:TimeEvent):void
        {
            info_txt.text='time: '+int(evt.target.currentTime)+'\n'+'duration: '+int(evt.target.duration);
        }

        private function destroy(evt:Event):void
        {
            removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }
    }
}

SWF ELEMENT

package com.flepstudio
{
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;

    import org.osmf.elements.SWFElement;
    import org.osmf.media.MediaPlayerSprite;
    import org.osmf.media.URLResource;

    public class Main extends Sprite
    {
        private const SWF:String="http://mediapm.edgesuite.net/osmf/content/test/ten.swf";

        private var url_resource:URLResource;

        private var video_sprite:MediaPlayerSprite;

        public function Main()
        {
            addEventListener(Event.ADDED_TO_STAGE,init);
            addEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }

        private function init(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);

            stage.scaleMode=StageScaleMode.NO_SCALE;
            stage.align=StageAlign.TOP_LEFT;

            createOSMFvideo();
        }

        private function createOSMFvideo():void
        {
            video_sprite=new MediaPlayerSprite();
            var swf_element:SWFElement=new SWFElement();
            url_resource=new URLResource(SWF);
            swf_element.resource=url_resource;

            addChild(video_sprite);
            video_sprite.media=swf_element;
        }

        private function destroy(evt:Event):void
        {
            removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }
    }
}

IMAGE ELEMENT

package com.flepstudio
{
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;

    import org.osmf.elements.ImageElement;
    import org.osmf.media.MediaPlayerSprite;
    import org.osmf.media.URLResource;

    public class Main extends Sprite
    {
        private const IMAGE:String="http://mediapm.edgesuite.net/strobe/content/test/train.jpg";

        private var url_resource:URLResource;

        private var video_sprite:MediaPlayerSprite;

        public function Main()
        {
            addEventListener(Event.ADDED_TO_STAGE,init);
            addEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }

        private function init(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);

            stage.scaleMode=StageScaleMode.NO_SCALE;
            stage.align=StageAlign.TOP_LEFT;

            createOSMFvideo();
        }

        private function createOSMFvideo():void
        {
            video_sprite=new MediaPlayerSprite();
            var image_element:ImageElement=new ImageElement();
            url_resource=new URLResource(IMAGE);
            image_element.resource=url_resource;

            addChild(video_sprite);
            video_sprite.media=image_element;
        }

        private function destroy(evt:Event):void
        {
            removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }
    }
}

MULTI VIDEO SEQUENCE

package com.flepstudio
{
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;

    import org.osmf.elements.*;
    import org.osmf.media.MediaPlayerSprite;
    import org.osmf.media.URLResource;

    public class Main extends Sprite
    {
        private var videos_array:Array=new Array("video_1.flv",
                                                                                 "video_2.flv",
                                                                                 "video_3.flv");

        private const DELAY:Number=1;

        private var serial_ement:SerialElement;

        private var video_sprite:MediaPlayerSprite;

        public function Main()
        {
            addEventListener(Event.ADDED_TO_STAGE,init);
            addEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }

        private function init(evt:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);

            stage.scaleMode=StageScaleMode.NO_SCALE;
            stage.align=StageAlign.TOP_LEFT;

            createOSMFvideo();
        }

        private function createOSMFvideo():void
        {
            video_sprite=new MediaPlayerSprite();
            video_sprite.width=stage.stageWidth;
            video_sprite.height=stage.stageHeight;
            serial_ement=new SerialElement();

            for(var i:int=0;i<videos_array.length;i++)
            {
                serial_ement.addChild(new VideoElement(new URLResource(videos_array[i])));
                serial_ement.addChild(new DurationElement(DELAY));
            }

            addChild(video_sprite);
            video_sprite.media=serial_ement;
        }

        private function destroy(evt:Event):void
        {
            removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
        }
    }
}

출처 : http://englishblog.flepstudio.org/tutorials/flash-cs3/mix/working-with-open-source-media-framework-osmf/ 

'AS3 > ActionScript' 카테고리의 다른 글

flixel 게임 프레임웍을 이용한 미니게임 만들기  (9) 2011.05.19
LocalConnection  (1) 2011.03.22
Actionscript 3 MySql Driver - assql  (0) 2011.03.14
as3.0 lib  (0) 2011.02.07
twitter API for ActionScrpit 3.0  (0) 2010.09.21