| cerber412 |
21.09.2017 17:00 |
Изменение размеров видео под размер окна
добрый день.
Помогите решить непростую проблему.
Есть скрипт проигрывания видео.
Код AS3:
var video:Video = new Video();
addChild(video);
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = {};
ns.client.onMetaData = ns_onMetaData;
ns.client.onCuePoint = ns_onCuePoint;
video.attachNetStream(ns);
ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");
function ns_onMetaData(item:Object):void {
trace("metaData");
// Resize video instance.
video.width = item.width;
video.height = item.height;
// Center video instance on Stage.
video.x = (stage.stageWidth - video.width) / 2;
video.y = (stage.stageHeight - video.height) / 2;
}
function ns_onCuePoint(item:Object):void {
trace("cuePoint");
trace(item.name + "\t" + item.time);
}
Пытаюсь добавить в этот скрипт, который определяет, исходя из текущих размеров и целевых, координаты и размеры вставляемой области (посредством экземпляра класса Rectangle).
Код AS3:
private function calculateFitRectangle(currentWidth:Number, currentHeight:Number, targetWidth:Number, targetHeight:Number):Rectangle {
var koefW:Number = targetWidth / currentWidth;
var koefH:Number = targetHeight / currentHeight;
var w:Number;
var h:Number;
var x:Number;
var y:Number;
if (koefH < 1 || koefW < 1) {
if (koefW < koefH) {
w = currentWidth * koefW;
h = currentHeight * koefW;
}else {
w = currentWidth * koefH;
h = currentHeight * koefH;
}
}else {
w = currentWidth;
h = currentHeight;
}
if (w < targetWidth) {
x = 0.5 * (targetWidth - w);
}else {
x = 0;
}
if (h < targetHeight) {
y = 0.5 * (targetHeight - h);
}else {
y = 0;
}
return new Rectangle(x, y, w, h);
}
private function calculateFitRectangleMaxSize(currentWidth:Number, currentHeight:Number, targetWidth:Number, targetHeight:Number):Rectangle {
var koefW:Number = targetWidth / currentWidth;
var koefH:Number = targetHeight / currentHeight;
var w:Number;
var h:Number;
var x:Number;
var y:Number;
if (koefH < 1 || koefW < 1) {
if (koefW < koefH) {
w = currentWidth * koefW;
h = currentHeight * koefW;
}else {
w = currentWidth * koefH;
h = currentHeight * koefH;
}
}else {
if (koefW < koefH) {
w = targetWidth;
h = currentHeight * w / currentWidth;
}else {
h = targetHeight;
w = currentWidth * h / currentHeight;
}
}
if (w < targetWidth) {
x = 0.5 * (targetWidth - w);
}else {
x = 0;
}
if (h < targetHeight) {
y = 0.5 * (targetHeight - h);
}else {
y = 0;
}
return new Rectangle(x, y, w, h);
}
Но что-то не работает.
Подскажите как правильно добавить второй код в первый код - что видео подстраивалось под размер любого окна (окно можно растягивать).
|