0

I'm trying to get the dimensions of the videosource in a VideoDisplay:

private function loadMovie () : void {
    vid = new VideoDisplay();
    vid.source = _item.itemLg;
    vid.play();
    vid.addEventListener ( MediaPlayerStateChangeEvent.MEDIA_PLAYER_STATE_CHANGE, onVideoPlay );
    addElement(vid);
}

private function onVideoPlay ( event : MediaPlayerStateChangeEvent ) : void {
    if ( event.state == MediaPlayerState.PLAYING ) {
           trace (vid.videoObject.width + " " + vid.videoObject.height);
    }
}

But the result is always 0.

I got the same result with:

trace (vid.videoObject.videoWidth + " " + vid.videoObject.videoHeight);

Any other idea?

Thanks

icke
  • 15
  • 4

3 Answers3

0

Assuming its a mx.controls.VideoDisplay, once the video loads you should be able to use:

trace (vid.videoWidth + " " + vid.videoHeight);

Without the videoObject in the middle.

You can also try listening for the ready event, which should fire on successful video load, instead of every time the state changes:

mx.events.VideoEvent.READY 
ToddBFisher
  • 11,370
  • 8
  • 38
  • 54
  • I'm using the spark.components.VideoDisplay. with the mx.controls.VideoDisplay I got the error: `Error: 1000: Unable to make connection to server or to find FLV on server.` – icke Nov 02 '11 at 12:01
  • I also tryed the VideoEvent.READY but I don't get it `private function loadMovie () : void { vid = new VideoDisplay(); vid.source = _item.itemLg; vid.addEventListener ( mx.events.VideoEvent.READY, onVideoReady ); addElement(vid); } private function onVideoReady ( event : mx.events.VideoEvent ) : void { trace (vid.videoObject.videoWidth + " " + vid.videoObject.videoHeight); vid.play(); }` – icke Nov 02 '11 at 12:09
0

I found a solution but I don't know why this works:

import org.osmf.events.MediaPlayerStateChangeEvent;
import org.osmf.media.MediaPlayerState;

import spark.components.VideoDisplay;

private var vid : VideoDisplay;
private var videoLoadCompleteTimer : Timer = new Timer (1, 5);

private function loadMovie () : void {
    vid = new VideoDisplay();
    vid.source = _item.itemLg;
    vid.autoPlay = false;
    vid.addEventListener ( MediaPlayerStateChangeEvent.MEDIA_PLAYER_STATE_CHANGE, onVideoPlay );
          addElement(vid);
}

private function onVideoPlay ( event : MediaPlayerStateChangeEvent ) : void {
    if ( event.state == MediaPlayerState.PLAYING ) {
        videoLoadCompleteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onVideoStart );
        videoLoadCompleteTimer.start();
    }
}

private function onVideoStart ( event : TimerEvent ) : void {
    trace(vid.videoObject.videoHeight + " " + vid.videoObject.videoWidth);
    vid.play();
    videoLoadCompleteTimer.reset();
}

I think it's a little bit dirty...

icke
  • 15
  • 4
  • Yeah using a timer is not the cleanest solution. Perhaps try MediaPlayerState.Ready. The timer solution works because by the time the timer fires onVideoStart() the videoObject is completely loaded. I haven't messed with spark components but in general when messing with videos I find it's all about finding the right events to listen for – ToddBFisher Nov 03 '11 at 03:56
0

I made a try with the READY event, but it works only sometimes. Mostly on the second or third click.

private function loadMovie () : void {
    vid = new VideoDisplay();
    vid.source = _item.itemLg;
    vid.autoPlay = false;
    vid.addEventListener ( MediaPlayerStateChangeEvent.MEDIA_PLAYER_STATE_CHANGE, onVideoPlay );
    addElement(vid);
}

private function onVideoPlay ( event : MediaPlayerStateChangeEvent ) : void {
    if ( event.state == MediaPlayerState.READY ) {
        vid.play();
    } else if ( event.state == MediaPlayerState.PLAYING ) {
        trace ("VidDimensions: " + vid.videoObject.videoWidth + " " + vid.videoObject.videoHeight);
        this.height = vid.videoObject.videoHeight;
        this.width = vid.videoObject.videoWidth;
        vid.move ( -vid.videoObject.videoWidth/2, -vid.videoObject.videoHeight/2 );
    }
}
icke
  • 15
  • 4