カテゴリー別アーカイブ: Video

ActionScript 3.0 ビデオプレイヤーで時間関連の値を取得・設定する

まずは, オーディオのトータル時間, つまり, HTML5のHTMLVideoElementのdurationプロパティに相当する値

var netConnection:NetConnection = new NetConnection();
netConnection.connect(null);

var netStream:NetStream = new NetStream(netConnection);
 
infoClients            = new Object();
infoClients.onMetaData = function onmetadata(info:Object):void {
    var duration:Number = info.duration;
};

netStream.client = infoClients;
 
netConnection.addEventListener(NetStatusEvent.NET_STATUS, onnetstatus, false, 0, true); 
netStream.addEventListener(NetStatusEvent.NET_STATUS,     onnetstatus, false, 0, true);
 
var video:Video = new Video();
video.attachNetStream(netStream);
addChild(video);

ビデオの現在再生位置, つまり, HTML5のHTMLVideoElementのcurrentTimeプロパティに相当する値. ただし, 取得と設定が異なります.

取得する場合は, NetStreamインスタンスのtimeプロパティンにアクセスします.

var currentTime:Number = netStream.time;

設定する場合は, NetStreamインスタンスのseekメソッドを利用します.

netStream.seek(currentTime + 1);

ActionScript 3.0 ビデオプレイヤークラス

ActionScript 3.0でビデオプレイヤーのためのクラスを作成してみた.

使い方はこんな感じ

import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import fl.motion.Color;
import classes.VideoPlayer

var video:VideoPlayer = new VideoPlayer();

var progress:TextField = new TextField();

var textFormat:TextFormat = new TextFormat();
textFormat.font = 'Helvetica';
textFormat.size = 24;
textFormat.color = 0x999999;
textFormat.align = TextFormatAlign.CENTER;

progress = new TextField();
progress.autoSize = TextFieldAutoSize.LEFT;
progress.defaultTextFormat = textFormat;

addChild(progress);

var duration:String = '00 : 00';
var currentTime:String = '00 : 00';

video.onerror = function(event:IOErrorEvent):void {
 var error:TextField = new TextField();
 error.defaultTextFormat = textFormat;
 error.text = event.text;

 addChild(error);
};

video.ondurationchange = function():void {
 var minutes:String = ('0' + Math.floor(video.duration / 60)).slice(-2);
 var seconds:String = ('0' + Math.floor(video.duration % 60)).slice(-2);

 duration = minutes + ' : ' + seconds;

 progress.text = currentTime + ' / ' + duration;
}

video.ontimeupdate = function():void {
 var minutes:String = ('0' + Math.floor(video.currentTime / 60)).slice(-2);
 var seconds:String = ('0' + Math.floor(video.currentTime % 60)).slice(-2);

 currentTime = minutes + ' : ' + seconds;

 progress.text = currentTime + ' / ' + duration;
};

video.onended = function(event:Event) {
 currentTime = '00 : 00';
}

video.src = 'kurenai-twin.mp4';

video.x = stage.stageWidth / 4;
video.y = stage.stageHeight / 4;

addChild(video);

createControlButtons();

function createControlButtons() {
 var playButton:Sprite = createButton(80, 20, 10, 2, 0x0066CC, 'Play', 0xFFFFFF);
 var pauseButton:Sprite = createButton(80, 20, 10, 2, 0x0066CC, 'Pause', 0xFFFFFF);
 var stopButton:Sprite = createButton(80, 20, 10, 2, 0x0066CC, 'Stop', 0xFFFFFF);

 playButton.x = 170;
 playButton.y = 365;

 pauseButton.x = 310;
 pauseButton.y = 365;

 stopButton.x = 450;
 stopButton.y = 365;

 playButton.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {video.play();}, false, 0, true);
 pauseButton.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {video.pause();}, false, 0, true);
 stopButton.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {}, false, 0, true);

 addChild(playButton);
 addChild(pauseButton);
 addChild(stopButton);
}

function createButton(width:Number, height:Number, radius:Number, lineWidth:Number, color:uint, text:String, textColor:uint):Sprite {
 var overColor:uint = Color.interpolateColor(color, 0xFFFFFF, 0.3);
 var downColor:uint = Color.interpolateColor(color, 0x000000, 0.3);

 var buttonWrapper:Sprite = new Sprite();

 var button:SimpleButton = new SimpleButton();

 //button.buttonMode = true;
 //button.useHandCursor = true;

 button.upState = createRoundButton(width, height, radius, lineWidth, color);
 button.overState = createRoundButton(width, height, radius, lineWidth, overColor);
 button.downState = createRoundButton(width, height, radius, lineWidth, downColor);
 button.hitTestState = button.upState;

 var textField:TextField = new TextField();

 var textFormat:TextFormat = new TextFormat();

 textFormat.font = 'Helvetica';
 textFormat.size = 10;
 textFormat.color = textColor;
 textFormat.align = TextFormatAlign.CENTER;

 textField.y = 4;
 textField.width = width;
 textField.height = height;
 textField.defaultTextFormat = textFormat;
 textField.text = text;

 textField.selectable = false;
 textField.mouseEnabled = false;

 buttonWrapper.addChild(button);
 buttonWrapper.addChild(textField);

 return buttonWrapper;
}

function createRoundButton(width:Number, height:Number, radius:Number, lineWidth:Number, color:uint):Sprite {
 var button:Sprite = new Sprite();

 button.graphics.lineStyle(lineWidth, color);
 button.graphics.beginFill(color, 0.5)
 button.graphics.drawRoundRect(0, 0, width, height, radius);
 button.graphics.endFill();

 return button;
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, function(event:KeyboardEvent):void {
 switch (event.keyCode) {
 case Keyboard.UP :
 video.volume += 0.05;
 break;
 case Keyboard.DOWN :
 video.volume -= 0.05;
 break;
 case Keyboard.SPACE :
 video.muted = !video.muted;
 trace('MUTE : ' + video.muted);
 break;
 case Keyboard.SHIFT :
 video.loop = !video.loop;
 trace('LOOP : ' + video.loop);
 break;
 case Keyboard.PERIOD :
 video.currentTime += 1;
 break;
 case Keyboard.COMMA :
 video.currentTime -= 1;
 break;
 default :
 break;
 }
}, false, 0, true);

HTML5 VideoをシミュレーションするようなAPIにしています.