2 Replies Latest reply on Jan 18, 2010 12:31 PM by gregbown

    Preload method question, durration and currentTime NaN

    gregbown Level 1

      I have found a curious behavior while experimenting with preload methods. I hope someone can enlighten me why and maybe how.

      I have had an ongoing issue with not being able to seek on a serial element set to MediaPlayerSprite.element. So a with a few traces I noticed that when the serial element seek trait is instanciated the time trait that is passed into it is null.  I decided to focus on preloading especially since the svn trunk version of ExamplePlayer has a functioning seek on a preloaded serial element. I tried to duplicate the method in ExamplePlayer to no avail. My method does not achieve the preload with neer identical code. The main differences are the switch and that I am mixing temporal elements with video elements.

      -------------------------------------------------------------------------------

      This method fails

              private function get media():MediaElement {
                  _serialElement = new SerialElement();
                  for(var contentNum:Number = 0; contentNum < _displayAssetsArray.length; contentNum++){
                      switch(_displayAssetsArray[contentNum][0]){
                          case "video":
                              trace("ELEMENT "+contentNum+" IS A VIDEO");
                              var videoElement:VideoElement = new VideoElement(new NetLoader(), new URLResource(new URL(_displayAssetsArray[contentNum][1])));
                              _referenceArray[contentNum] = videoElement; 
                                  function preloadVideo(videoElement:VideoElement):void {
                                      var videoLoadTrait:LoadTrait = videoElement.getTrait(MediaTraitType.LOAD) as LoadTrait;
                                      videoLoadTrait.addEventListener(LoadEvent.LOAD_STATE_CHANGE, onVideoLoad);
                                      videoLoadTrait.load();
                                      function onVideoLoad(event:LoadEvent):void {
                                          if (event.loadState == LoadState.READY){
                                              _loadCount++;
                                              videoLoadTrait.removeEventListener(LoadEvent.LOAD_STATE_CHANGE, onVideoLoad);
                                              var playTrait:PlayTrait = videoElement.getTrait(MediaTraitType.PLAY) as PlayTrait;
                                              playTrait.play();
                                              playTrait.pause();
                                              _timeTrait = videoElement.getTrait(MediaTraitType.TIME)as TimeTrait;
                                              trace("VIDEO TIME DURATION:"+_timeTrait.duration+" CURRENT TIME:"+_timeTrait.currentTime);
                                          }
                                      }
                                  }
                                  preloadVideo(videoElement);
                                  _serialElement.addChild(videoElement);
                             
                              //var vf:KeyValueFacet = new KeyValueFacet();
                              //vf.addValue(new ObjectIdentifier("index"),contentNum);
                              //videoElement.metadata.addFacet(vf);
                              break;

      ------------------------------------------------------------------------------------------ -

      This method works... well sort of.

              private function preloadVideo(videoElement:VideoElement):void {
                  _loadTrait = videoElement.getTrait(MediaTraitType.LOAD) as LoadTrait;
                  _loadTrait.addEventListener(LoadEvent.LOAD_STATE_CHANGE, function(event:LoadEvent):void {
                      if (event.loadState == LoadState.READY){
                          _loadCount++;
                          _loadTrait.removeEventListener(LoadEvent.LOAD_STATE_CHANGE, function():void{});
                          _serialElement.addChild(videoElement);
                          _playTrait = _serialElement.getTrait(MediaTraitType.PLAY) as PlayTrait;
                          _playTrait.play();
                          trace("loaded a video:"+_loadCount);
                          _playTrait.pause();
                          _timeTrait = _serialElement.getTrait(MediaTraitType.TIME)as TimeTrait;
                          trace("VIDEO TIME DURATION:"+_timeTrait.duration+" CURRENT TIME:"+_timeTrait.currentTime);
                          if(_loadCount == 4){
                              trace("SET MEDIA PLAYER ELEMENT TO SERIAL ELEMENT");
                              mediaContainer.element = _serialElement;
                          }
                      }
                  });
                  _loadTrait.load();
              }

      ------------------------------------------------------

      In the second method I add the video to the serial element and get the play trait on the serial element.  I should mention there is one catch, I must still allow all the media to paly through once because until that happens I can only seek within temporal elements that have loaded to the play state while on the display list.

       

      In the first method the time trait remains null inside seek trait even if all media is played back in full so seek fails on media player and serial element.

      It will work on the actual video element with videoElement.getTrait. And debug mode reveals this is the only level that the time trait is not null.

       

      In the second method durration and current time are set to default values untill media enters the play state while on the display list. From the first second of playback I can emmediately seek to the end of that element. I can also seek to any point of prior elements in the serial element.

       

      I am baffled as to why it works in ExamplePlayer?

      How is that my time trait comes up null even though if it is retrieved on the video element it has all the metadata along with accurate durration?

      In ExamplePlayer the time trait is not null as it is when I tried to copy the method?

       

      I know bugs have already been filed so thank you for any advice, direction or incite.

      Greg

        • 1. Re: Preload method question, durration and currentTime NaN
          bringrags Level 4

          Rather than getting the TimeTrait immediately after calling play() and pause(), you should add a listener for the TimeTrait being added (MediaElementEvent.TRAIT_ADD)  The addition of the TimeTrait happens asynchronously (since it's based on the onMetadata callback, which also happens asynchronously), so when you call getTrait, the TimeTrait doesn't actually exist yet.

          • 2. Re: Preload method question, durration and currentTime NaN
            gregbown Level 1

            Thank you, however the problem is that unless I add the video or temporal element to the serial element within the preload the media player never sees it as seekable. This leaves me at the mercy of the load sequence and unable to compose my composition element. I have tried variouse methods of addChildAt too.

            My questions or post is more about why is the event never recieved in the case where my method is so similar to ExamplePlayer. What is the secret ingredient? I guess I am frustrated because if I compose the serial element outside of preloading I can see the trait being set on the child element and serial element in the debuger but I can't isolate why it never gets beyond that point.

             

            Waiting for the time trait does not give me a way out of the confinement to the preload. I appologize for my lack of clarity.

            Thanks again.

            Greg