動画情報取得とコントロール

by yun forked from 動画の再生 (diff: 118)
♥0 | Line 128 | Modified 2010-10-05 19:44:19 | MIT License
play

ActionScript3 source code

/**
 * Copyright yun ( http://wonderfl.net/user/yun )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/oba9
 */

// forked from yun's 動画の再生
package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.NetStatusEvent;
    import flash.media.Video;
    import flash.net.FileReference;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.system.Security;
    
    public class Main extends Sprite
    {
        private var _connection:NetConnection;
        private var _stream:NetStream;
        private var _video:Video = new Video;
        private var _metaDataTxt:TextField = new TextField;
        private var _netStatusTxt:TextField = new TextField;
        private var _enterFrameTxt:TextField = new TextField;
        private var _playPauseButton:PlayAndPauseButton = new PlayAndPauseButton;
        private var _videoURL:String = "http://www.muraken.biz/wonderfl/construction.mp4";
        
        public function Main():void
        {
            Security.loadPolicyFile( "http://www.muraken.biz/crossdomain.xml" );
            
            addChild( _video );
            addChild( _metaDataTxt );
            addChild( _netStatusTxt );
            addChild( _enterFrameTxt );
            addChild( _playPauseButton );
            _playPauseButton.visible = false;
            
            _netStatusTxt.y = 20;
            _enterFrameTxt.y = 40;            
            
            _metaDataTxt.autoSize = TextFieldAutoSize.LEFT;
            _netStatusTxt.autoSize = TextFieldAutoSize.LEFT;
            _enterFrameTxt.autoSize = TextFieldAutoSize.LEFT;
            
            _video.y = 130;
            
            _connection = new NetConnection();
            _connection.addEventListener( NetStatusEvent.NET_STATUS, _netStatusHandler );
            _connection.connect( null );
        }
        
        private function _netStatusHandler( e:NetStatusEvent ):void
        {
            _netStatusTxt.text = "NetStatus: " + e.info.code;
            if( e.info.code=="NetConnection.Connect.Success" )_connectStream();
            else if( e.info.code=="NetStream.Play.Stop" )_stream.seek( 0 );
        }
        
        private function _connectStream():void
        {
            _stream = new NetStream( _connection );
            _stream.addEventListener( NetStatusEvent.NET_STATUS, _netStatusHandler );
            _stream.client = this;
            _stream.bufferTime = 3;
            
            _video.x = Math.floor( ( stage.stageWidth - _video.width ) / 2 );
            _video.y = Math.floor( ( stage.stageHeight - _video.height ) / 2 );
            _video.attachNetStream( _stream );
            
            _stream.play( _videoURL );
            _playPauseButton.y = _video.y + _video.height + 20;
            _playPauseButton.x = _video.x + Math.floor( ( _video.width - _playPauseButton.width ) / 2 );
            _playPauseButton.visible = true;
            _playPauseButton.addEventListener( MouseEvent.CLICK, _onClick );
            addEventListener( Event.ENTER_FRAME, _onEnterFrame );
        }
        
        private function _onEnterFrame( e:Event ):void
        {
            _enterFrameTxt.text = "再生位置:" + _stream.time + "秒|バッファ時間:" + _stream.bufferTime + "秒";
        }
        
        private function _onClick( e:MouseEvent ):void
        {
            _stream.togglePause();
            _playPauseButton.toggleTex();
        }

        // callback
        public function onMetaData( info:Object ):void
        {
            _metaDataTxt.text = "ビデオの尺:" + info.duration + "秒|ビデオの幅:" + info.width + "px|ビデオの高さ:" + info.height + "px";
        }
        public function onCuePoint( info:Object ):void {}
        public function onXMPData( info:Object ):void {}
    } 
}

import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;

class PlayAndPauseButton extends Sprite
{
    private var _bg:Shape = new Shape;
    private var _playTex:Shape = new Shape;
    private var _pauseTex:Shape = new Shape;
    private var _ctfRed:ColorTransform = new ColorTransform( 0, 0, 0, 1, 255, 0, 0, 0 );
    private var _ctfDef:ColorTransform = new ColorTransform;
    
    public function PlayAndPauseButton()
    {
        addChild( _bg );
        addChild( _playTex );
        addChild( _pauseTex );
        
        _bg.graphics.beginFill( 0 );
        _bg.graphics.drawRect( 0, 0, 40, 24 );
        _bg.graphics.endFill();
        
        _playTex.graphics.beginFill( 0xffffff );
        _playTex.graphics.moveTo( 30, 12 );
        _playTex.graphics.lineTo( 10, 5 );
        _playTex.graphics.lineTo( 10, 19 );
        _playTex.graphics.endFill();
        
        _pauseTex.graphics.beginFill( 0xffffff );
        _pauseTex.graphics.drawRect( 12, 5, 5, 14 );
        _pauseTex.graphics.drawRect( 24, 5, 5, 14 );
        _pauseTex.graphics.endFill();
        _playTex.visible = false;
        
        addEventListener( MouseEvent.ROLL_OVER, _onRollOver );
        addEventListener( MouseEvent.ROLL_OUT, _onRollOut );
        
        buttonMode = true;
    }
    
    private function _onRollOut( e:MouseEvent ):void
    {
        _bg.transform.colorTransform = _ctfDef;
    }
    
    private function _onRollOver( e:MouseEvent ):void
    {
        _bg.transform.colorTransform = _ctfRed;
    }
    public function toggleTex():void
    {
        _playTex.visible = !_playTex.visible;
        _pauseTex.visible = !_pauseTex.visible;
    }
}

Forked