Форум Flasher.ru

Форум Flasher.ru (http://www.flasher.ru/forum/index.php)
-   ActionScript 3.0 (http://www.flasher.ru/forum/forumdisplay.php?f=83)
-   -   Изменение размеров видео под размер окна (http://www.flasher.ru/forum/showthread.php?t=214559)

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);
        }

Но что-то не работает.
Подскажите как правильно добавить второй код в первый код - что видео подстраивалось под размер любого окна (окно можно растягивать).

Nooob 21.09.2017 23:44

Код AS3:

                        video.width = stage.stageWidth;
                        video.height = stage.stageHeight;
                        stage.scaleMode = StageScaleMode.SHOW_ALL;

или stage.scaleMode = StageScaleMode.NO_BORDER; в зависимости от того что надо

undefined 22.09.2017 09:58

Цитата:

Но что-то не работает.
Подскажите как правильно добавить второй код в первый код - что видео подстраивалось под размер любого окна (окно можно растягивать).
Код AS3:

onResize();
stage.addEventListener(Event.RESIZE,onResize);
function onResize(e:Event=null):void{
    var rect:Rectangle=calculateFitRectangleMaxSize(stage.stageWidth, stage.stageHeight, targetWidth, targetHeight);
    video.width = rect.width;
    video.height = rect.height;
}


cerber412 23.09.2017 15:04

Nooob , undefined - теперь все работает.
Спасибо за ответы.


Часовой пояс GMT +4, время: 01:21.

Copyright © 1999-2008 Flasher.ru. All rights reserved.
Работает на vBulletin®. Copyright ©2000 - 2026, Jelsoft Enterprises Ltd. Перевод: zCarot
Администрация сайта не несёт ответственности за любую предоставленную посетителями информацию. Подробнее см. Правила.