PDA

View Full Version : Resizing dynamic content proportionally



jasonhardwick
January 27th, 2009, 01:34 PM
I have a SWF that pulls in movie files. I need a way to detect the size of the file that is being pulled in and scaling it appropriately... I have a feeling that this might be a little complicated, but maybe one of you guru's can help me out below is my AS3 code that pulls and displays my movies.

Thanks




import caurina.transitions.*;

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

var stream:NetStream = new NetStream(conn);

var ID:String = stage.loaderInfo.parameters.ID;

if( ID != null )
{
output.text = "Your Movie Is Loading.... Please Wait";
}
else
{
output.text = "This Movie Is Comming Soon";
}

//stream.play("97363-49264.mp4");
stream.play(ID);


var metaListener:Object = new Object();
metaListener.onMetaData = theMeta;
stream.client = metaListener;

var st:SoundTransform = new SoundTransform();
stream.soundTransform = st;

var video:Video = new Video();
video.attachNetStream(stream);

//myMovieClip_MC.width = 300;
video.width = 780;
//video.scaleY = video.scaleX;
video.height = 370;

video.x = 0;
video.y = 0;
video_mc.addChild(video);

barBg_mc.thumb_mc.mouseEnabled = false;
barBg_mc.thumb_mc.alpha = 0;
barBg_mc.track_mc.buttonMode = true;
barBg_mc.speaker_mc.buttonMode = true;
barBg_mc.toggle_mc.buttonMode = true;
bigPlay_mc.buttonMode = true;
barBg_mc.volScrubber_mc.volThumb_mc.buttonMode = true;

stage.addEventListener(Event.ENTER_FRAME, enterFrame);
stage.addEventListener(MouseEvent.MOUSE_OVER, getInterface);
stage.addEventListener(MouseEvent.MOUSE_OUT, removeInterface);
barBg_mc.speaker_mc.addEventListener(MouseEvent.CL ICK, mute);
barBg_mc.speaker_mc.addEventListener(MouseEvent.MO USE_OVER, rollOnSpeaker);
barBg_mc.speaker_mc.addEventListener(MouseEvent.MO USE_OUT, rollOffSpeaker);
barBg_mc.toggle_mc.addEventListener(MouseEvent.CLI CK, pause);
barBg_mc.toggle_mc.addEventListener(MouseEvent.MOU SE_OVER, rollOnToggle);
barBg_mc.toggle_mc.addEventListener(MouseEvent.MOU SE_OUT, rollOffToggle);
bigPlay_mc.addEventListener(MouseEvent.CLICK, pause);
barBg_mc.track_mc.addEventListener(MouseEvent.MOUS E_OVER, trackOver);
barBg_mc.track_mc.addEventListener(MouseEvent.MOUS E_OUT, trackOut);
barBg_mc.track_mc.addEventListener(MouseEvent.CLIC K, goToSecond);
barBg_mc.track_mc.addEventListener(MouseEvent.MOUS E_DOWN, trackDown);
barBg_mc.track_mc.addEventListener(MouseEvent.MOUS E_UP, trackUp);
barBg_mc.volScrubber_mc.volThumb_mc.addEventListen er(MouseEvent.MOUSE_DOWN, volDown);
stage.addEventListener(MouseEvent.MOUSE_UP, volUp);

var xOffset:Number;
var xMin:Number = 13;
var xMax:Number = 309;
var volOffset:Number;
var volxMin:Number = 0;
var volxMax:Number = barBg_mc.volScrubber_mc.volTrack_mc.width - 7;
var volPercent:Number;
var totalLength:uint;

function theMeta(data:Object):void
{
totalLength = data.duration;
}

function enterFrame(e:Event):void
{
var nowSecs:Number = Math.floor(stream.time);
var totalSecs:Number = Math.round(totalLength);
if(nowSecs > 0)
{
barBg_mc.timerText.text = videoTimeConvert(nowSecs) + " / " + videoTimeConvert(totalSecs);
var amountPlayed:Number = stream.time / totalLength;
var amountLoaded:Number = stream.bytesLoaded / stream.bytesTotal;
barBg_mc.playStatus_mc.x = 46;
barBg_mc.playStatus_mc.width = 294 * amountPlayed - 1;
barBg_mc.dlStatus_mc.x = 46;
barBg_mc.dlStatus_mc.width = 294 * amountLoaded + 3;
}
}

function getInterface(e:MouseEvent):void
{
if(mouseX > 70 && mouseX < 710 && mouseY > 0 && mouseY < 360)
Tweener.addTween(barBg_mc, {alpha:1, time:3});
}

function removeInterface(e:MouseEvent):void
{
Tweener.addTween(barBg_mc, {alpha:0, time:3});
}

function trackOver(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, startFollow);
xOffset = mouseX - barBg_mc.thumb_mc.x;
}

function trackOut(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, startFollow);
barBg_mc.thumb_mc.alpha = 0;
}

function startFollow(e:MouseEvent):void
{
barBg_mc.thumb_mc.alpha = 1;
barBg_mc.thumb_mc.x = barBg_mc.mouseX - (barBg_mc.thumb_mc.width / 2) + 2;
if(barBg_mc.thumb_mc.x <= xMin)
barBg_mc.thumb_mc.x = xMin;
if(barBg_mc.thumb_mc.x >= xMax)
barBg_mc.thumb_mc.x = xMax;
stage.addEventListener(Event.ENTER_FRAME, getTimeText);
e.updateAfterEvent();
}

function getTimeText(e:Event):void
{
var percentAcross:Number = (barBg_mc.thumb_mc.x - 12) / barBg_mc.track_mc.width;
barBg_mc.thumb_mc.trackTime_mc.text = videoTimeConvert(totalLength * percentAcross);
}

function goToSecond(e:MouseEvent):void
{
if(barBg_mc.track_mc.mouseX < barBg_mc.dlStatus_mc.width)
{
var percentAcross:Number = (barBg_mc.thumb_mc.x - 12) / barBg_mc.track_mc.width;
stream.seek(totalLength * percentAcross);
}
}

function trackDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, scrubTo);
xOffset = mouseX - barBg_mc.thumb_mc.x;
}

function trackUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, scrubTo);
}

function scrubTo(e:MouseEvent):void
{
if(barBg_mc.track_mc.mouseX < barBg_mc.dlStatus_mc.width)
{
var percentAcross:Number = (barBg_mc.thumb_mc.x - 12) / barBg_mc.track_mc.width;
stream.seek(totalLength * percentAcross);
}
}

function pause(e:MouseEvent):void
{
stream.togglePause();
if(barBg_mc.toggle_mc.currentFrame == 1)
{
bigPlay_mc.alpha = 1;
barBg_mc.toggle_mc.gotoAndStop(2);
}
else
{
bigPlay_mc.alpha = 0;
barBg_mc.toggle_mc.gotoAndStop(1);
}
}

function rollOnToggle(e:MouseEvent):void
{
barBg_mc.toggle_mc.alpha = .5;
}

function rollOffToggle(e:MouseEvent):void
{
barBg_mc.toggle_mc.alpha = 1;
}

function mute(e:MouseEvent):void
{
if(barBg_mc.speaker_mc.currentFrame == 1)
{
st.volume = 0;
stream.soundTransform = st;
barBg_mc.speaker_mc.gotoAndStop(2);
}
else
{
st.volume = volPercent;
stream.soundTransform = st;
barBg_mc.speaker_mc.gotoAndStop(1);
}
}

function rollOnSpeaker(e:MouseEvent):void
{
barBg_mc.speaker_mc.alpha = .5;
}

function rollOffSpeaker(e:MouseEvent):void
{
barBg_mc.speaker_mc.alpha = 1;
}

function volDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, volAdjust);
}

function volUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, volAdjust);
}

function volAdjust(e:MouseEvent):void
{
barBg_mc.volScrubber_mc.volThumb_mc.x = barBg_mc.volScrubber_mc.volTrack_mc.mouseX;
if(barBg_mc.volScrubber_mc.volThumb_mc.x <= volxMin)
barBg_mc.volScrubber_mc.volThumb_mc.x = volxMin;
if(barBg_mc.volScrubber_mc.volThumb_mc.x >= volxMax)
barBg_mc.volScrubber_mc.volThumb_mc.x = volxMax;
volPercent = barBg_mc.volScrubber_mc.volThumb_mc.x / volxMax;
if(barBg_mc.speaker_mc.currentFrame == 1)
st.volume = volPercent;
stream.soundTransform = st;
e.updateAfterEvent();
}

var displayHours:Boolean = true;

function videoTimeConvert(myTime):String
{
var tempNum = myTime;
var minutes = Math.floor(tempNum / 60);

if (displayHours)
{
var hours = Math.floor(minutes / 60);
}
var seconds = Math.round(tempNum - (minutes * 60));

if (seconds < 10)
{
seconds = "0" + seconds;
}
if (minutes < 10)
{
minutes = "0" + minutes;
}

if (displayHours)
{
if (hours < 10)
{
hours = "0" + hours;
}
}

var currentTimeConverted = hours + ":" + minutes + ":" + seconds;

return currentTimeConverted;
}

jasonhardwick
January 27th, 2009, 09:20 PM
Please... any help or even a direction?

creatify
January 27th, 2009, 10:01 PM
can you isolate and post the section of your code where the scaling needs to happen?

.ral:cr
January 28th, 2009, 01:54 AM
well, the code is indeed big and requires time to see what is doing. maybe was a little easier if you just putted it in AS tags.
Anyway, the size of the movie should be available when the metadata is available.

jasonhardwick
January 28th, 2009, 10:31 AM
Thanks...

Here is the section that sizes the movie.

video.width = 780;
//video.scaleY = video.scaleX;
video.height = 370;

video.x = 0;
video.y = 0;
video_mc.addChild(video);

jasonhardwick
January 28th, 2009, 10:32 AM
Also here is the Whole code in AS
import caurina.transitions.*;

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

var stream:NetStream = new NetStream(conn);

var ID:String = stage.loaderInfo.parameters.ID;

if( ID != null )
{
output.text = "Your Movie Is Loading.... Please Wait";
}
else
{
output.text = "This Movie Is Comming Soon";
}

//stream.play("97363-49264.mp4");
stream.play(ID);


var metaListener:Object = new Object();
metaListener.onMetaData = theMeta;
stream.client = metaListener;

var st:SoundTransform = new SoundTransform();
stream.soundTransform = st;

var video:Video = new Video();
video.attachNetStream(stream);

//myMovieClip_MC.width = 300;
video.width = 780;
//video.scaleY = video.scaleX;
video.height = 370;

video.x = 0;
video.y = 0;
video_mc.addChild(video);

barBg_mc.thumb_mc.mouseEnabled = false;
barBg_mc.thumb_mc.alpha = 0;
barBg_mc.track_mc.buttonMode = true;
barBg_mc.speaker_mc.buttonMode = true;
barBg_mc.toggle_mc.buttonMode = true;
bigPlay_mc.buttonMode = true;
barBg_mc.volScrubber_mc.volThumb_mc.buttonMode = true;

stage.addEventListener(Event.ENTER_FRAME, enterFrame);
stage.addEventListener(MouseEvent.MOUSE_OVER, getInterface);
stage.addEventListener(MouseEvent.MOUSE_OUT, removeInterface);
barBg_mc.speaker_mc.addEventListener(MouseEvent.CL ICK, mute);
barBg_mc.speaker_mc.addEventListener(MouseEvent.MO USE_OVER, rollOnSpeaker);
barBg_mc.speaker_mc.addEventListener(MouseEvent.MO USE_OUT, rollOffSpeaker);
barBg_mc.toggle_mc.addEventListener(MouseEvent.CLI CK, pause);
barBg_mc.toggle_mc.addEventListener(MouseEvent.MOU SE_OVER, rollOnToggle);
barBg_mc.toggle_mc.addEventListener(MouseEvent.MOU SE_OUT, rollOffToggle);
bigPlay_mc.addEventListener(MouseEvent.CLICK, pause);
barBg_mc.track_mc.addEventListener(MouseEvent.MOUS E_OVER, trackOver);
barBg_mc.track_mc.addEventListener(MouseEvent.MOUS E_OUT, trackOut);
barBg_mc.track_mc.addEventListener(MouseEvent.CLIC K, goToSecond);
barBg_mc.track_mc.addEventListener(MouseEvent.MOUS E_DOWN, trackDown);
barBg_mc.track_mc.addEventListener(MouseEvent.MOUS E_UP, trackUp);
barBg_mc.volScrubber_mc.volThumb_mc.addEventListen er(MouseEvent.MOUSE_DOWN, volDown);
stage.addEventListener(MouseEvent.MOUSE_UP, volUp);

var xOffset:Number;
var xMin:Number = 13;
var xMax:Number = 309;
var volOffset:Number;
var volxMin:Number = 0;
var volxMax:Number = barBg_mc.volScrubber_mc.volTrack_mc.width - 7;
var volPercent:Number;
var totalLength:uint;

function theMeta(data:Object):void
{
totalLength = data.duration;
}

function enterFrame(e:Event):void
{
var nowSecs:Number = Math.floor(stream.time);
var totalSecs:Number = Math.round(totalLength);
if(nowSecs > 0)
{
barBg_mc.timerText.text = videoTimeConvert(nowSecs) + " / " + videoTimeConvert(totalSecs);
var amountPlayed:Number = stream.time / totalLength;
var amountLoaded:Number = stream.bytesLoaded / stream.bytesTotal;
barBg_mc.playStatus_mc.x = 46;
barBg_mc.playStatus_mc.width = 294 * amountPlayed - 1;
barBg_mc.dlStatus_mc.x = 46;
barBg_mc.dlStatus_mc.width = 294 * amountLoaded + 3;
}
}

function getInterface(e:MouseEvent):void
{
if(mouseX > 70 && mouseX < 710 && mouseY > 0 && mouseY < 360)
Tweener.addTween(barBg_mc, {alpha:1, time:3});
}

function removeInterface(e:MouseEvent):void
{
Tweener.addTween(barBg_mc, {alpha:0, time:3});
}

function trackOver(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, startFollow);
xOffset = mouseX - barBg_mc.thumb_mc.x;
}

function trackOut(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, startFollow);
barBg_mc.thumb_mc.alpha = 0;
}

function startFollow(e:MouseEvent):void
{
barBg_mc.thumb_mc.alpha = 1;
barBg_mc.thumb_mc.x = barBg_mc.mouseX - (barBg_mc.thumb_mc.width / 2) + 2;
if(barBg_mc.thumb_mc.x <= xMin)
barBg_mc.thumb_mc.x = xMin;
if(barBg_mc.thumb_mc.x >= xMax)
barBg_mc.thumb_mc.x = xMax;
stage.addEventListener(Event.ENTER_FRAME, getTimeText);
e.updateAfterEvent();
}

function getTimeText(e:Event):void
{
var percentAcross:Number = (barBg_mc.thumb_mc.x - 12) / barBg_mc.track_mc.width;
barBg_mc.thumb_mc.trackTime_mc.text = videoTimeConvert(totalLength * percentAcross);
}

function goToSecond(e:MouseEvent):void
{
if(barBg_mc.track_mc.mouseX < barBg_mc.dlStatus_mc.width)
{
var percentAcross:Number = (barBg_mc.thumb_mc.x - 12) / barBg_mc.track_mc.width;
stream.seek(totalLength * percentAcross);
}
}

function trackDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, scrubTo);
xOffset = mouseX - barBg_mc.thumb_mc.x;
}

function trackUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, scrubTo);
}

function scrubTo(e:MouseEvent):void
{
if(barBg_mc.track_mc.mouseX < barBg_mc.dlStatus_mc.width)
{
var percentAcross:Number = (barBg_mc.thumb_mc.x - 12) / barBg_mc.track_mc.width;
stream.seek(totalLength * percentAcross);
}
}

function pause(e:MouseEvent):void
{
stream.togglePause();
if(barBg_mc.toggle_mc.currentFrame == 1)
{
bigPlay_mc.alpha = 1;
barBg_mc.toggle_mc.gotoAndStop(2);
}
else
{
bigPlay_mc.alpha = 0;
barBg_mc.toggle_mc.gotoAndStop(1);
}
}

function rollOnToggle(e:MouseEvent):void
{
barBg_mc.toggle_mc.alpha = .5;
}

function rollOffToggle(e:MouseEvent):void
{
barBg_mc.toggle_mc.alpha = 1;
}

function mute(e:MouseEvent):void
{
if(barBg_mc.speaker_mc.currentFrame == 1)
{
st.volume = 0;
stream.soundTransform = st;
barBg_mc.speaker_mc.gotoAndStop(2);
}
else
{
st.volume = volPercent;
stream.soundTransform = st;
barBg_mc.speaker_mc.gotoAndStop(1);
}
}

function rollOnSpeaker(e:MouseEvent):void
{
barBg_mc.speaker_mc.alpha = .5;
}

function rollOffSpeaker(e:MouseEvent):void
{
barBg_mc.speaker_mc.alpha = 1;
}

function volDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, volAdjust);
}

function volUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, volAdjust);
}

function volAdjust(e:MouseEvent):void
{
barBg_mc.volScrubber_mc.volThumb_mc.x = barBg_mc.volScrubber_mc.volTrack_mc.mouseX;
if(barBg_mc.volScrubber_mc.volThumb_mc.x <= volxMin)
barBg_mc.volScrubber_mc.volThumb_mc.x = volxMin;
if(barBg_mc.volScrubber_mc.volThumb_mc.x >= volxMax)
barBg_mc.volScrubber_mc.volThumb_mc.x = volxMax;
volPercent = barBg_mc.volScrubber_mc.volThumb_mc.x / volxMax;
if(barBg_mc.speaker_mc.currentFrame == 1)
st.volume = volPercent;
stream.soundTransform = st;
e.updateAfterEvent();
}

var displayHours:Boolean = true;

function videoTimeConvert(myTime):String
{
var tempNum = myTime;
var minutes = Math.floor(tempNum / 60);

if (displayHours)
{
var hours = Math.floor(minutes / 60);
}
var seconds = Math.round(tempNum - (minutes * 60));

if (seconds < 10)
{
seconds = "0" + seconds;
}
if (minutes < 10)
{
minutes = "0" + minutes;
}

if (displayHours)
{
if (hours < 10)
{
hours = "0" + hours;
}
}

var currentTimeConverted = hours + ":" + minutes + ":" + seconds;

return currentTimeConverted;
}

.ral:cr
January 28th, 2009, 12:56 PM
try this:


stream.client = this;
function onMetaData (pMeta) :void {

trace(pMeta.duration);
trace(pMeta.width)
trace(pMeta.height)

}

and watch if you have width and height, may be missing sometimes.
or you're asking about the mathematics you have to use to scale it proportionaly?

jasonhardwick
January 28th, 2009, 07:05 PM
I'm asking about the mathematics of it

creatify
January 28th, 2009, 08:05 PM
So, are you wanting to force the native movie to a fixed width but a proportional height?

Here is an explanation of the math. If you want your movie to end up at a width of 720, but let the height just end up proportional:

In the metadata, you'll need the native width and height, as he'd posted above, so you'll store those when it's retrieved. Let's say that native width is 480, height is 220.

Then, you know the width you want to make it, 720.

So, (newWidth/nativeWidth) = ratio;
720/480 = 1.5

Then you can, maybe, set the scaleY*1.5

Or to get the new proportional height in pixels multiply the nativeHeight*1.5
220x1.5 = 330

So, native video was 480x220, scaled in flash it is 720x330.

jasonhardwick
January 29th, 2009, 10:21 AM
Can anyone show me how this would be places into my script.... I get the concept but were should all the Var's go so it it called properly....


Thanks....