7 Replies Latest reply on Apr 13, 2010 8:36 PM by bringrags

    Whats is the strategy for starting a video in the 5th second ?

    Soluciones1A

      I havent been able to find the correct strategy to implement this simple use case:

       

      I need to play a local video, but i want the playhead to start int the 5th second, not in the first frame, i have tryied unsuccesfully using the seek method, pausing the video rigth after first playing event... but no luck.

       

      Please letme know what is the best strategy for this, im using OSMF directly becouse looking at the code of the spark VideoDisplay it is just a wrapper and the same problem arises, i have even implemented my own version of VideoDisplay but it works with some videos but not with otehrs, it seems random.

       

      Any pointers in how to achive this? thanks in advance.

       

      pd. the 5th second is just an example in production its an arbritrary time, im using flash builder 4.

        • 1. Re: Whats is the strategy for starting a video in the 5th second ?
          bringrags Level 4

          For local/progressive videos, this isn't easy to do.  (For streaming videos, it's very easy, just set StreamingURLResource.clipStartTime to 5.)  If I were implementing this, then I would have the "load" operation (encapsulated by the LoadTrait) execute a mute-play-pause-seek(5)-wait-unmute prior to signalling that it's READY (loaded).  If you're want the video to be constrained such that the user can never seek back before second #5, then you'd need to override the duration and currentTime properties (via the TimeTrait), the seek target time (via the SeekTrait), etc.  It's doable, but it's a chunk of work.  We have it on our backlog as a post-1.0 feature.

          • 2. Re: Whats is the strategy for starting a video in the 5th second ?
            Soluciones1A Level 1

            Please elaborate more on this (im not osmf expert, not yet ):

            If I were implementing this, then I would have the "load" operation (encapsulated by the LoadTrait) execute a mute-play-pause-seek(5)-wait-unmute prior to signalling that it's READY (loaded).

            What should i do in orther to do this "mute-play-pause-seek(5)-wait-unmute"

             

            • Extend VideoElement?
            • Extend MediaPlayer?
            • Implement my own VideoElement?
            • Implement my own MediaPlayer?
              • Override LoadableMediaElement.processLoadingState()

             

            this is my sample code so far:

             

            canvas = new RegionGateway();
            canvas.clipChildren = true;
            canvas.x = 0; canvas.y = 0; canvas.backgroundColor = 0x00ff00;               
            LayoutUtils.setAbsoluteLayout(canvas.metadata, 720, 405);
            LayoutUtils.setLayoutAttributes(canvas.metadata, ScaleMode.ZOOM, RegistrationPoint.BOTTOM_LEFT);
            sve.addChild(canvas);

             

            mediaPlayer = new MediaPlayer();
            mediaPlayer.autoPlay = true;

             

            var path:String = "myvideo.flv";

            videoElement = new VideoElement
                ( new NetLoader()
                    , new URLResource(new URL("file://"+path))
                );
            videoElement.gateway = canvas;
            mediaPlayer.element = videoElement;
            //mediaPlayer.pause();
            mediaPlayer.seek(5);
            mediaPlayer.addEventListener(SeekEvent.SEEK_END,onSeek);
            mediaPlayer.addEventListener(MediaPlayerStateChangeEvent.MEDIA_PLAYER_STATE_CHANGE,mpsc);

            • 3. Re: Whats is the strategy for starting a video in the 5th second ?
              bringrags Level 4

              I would do this:

               

              1. Set MediaPlayer.muted to true.

              2. Add a listener for the playStateChange event, and then call play().

              3. When you get the playStateChange  event (for PLAYING), call pause().

              4. When you get the next playStateChange event (for PAUSED), add a listener for the seekingChange event, and call seek(5).

              5. You'll get two seekingChange events, one with seeking true, then another with seeking false (when the seek completes).  Upon receipt of the second event, set muted to false.

               

              I would try to implement this externally against the MediaPlayer API (similar to what you're doing) first, to see if you can get the behavior working well.  Once you've done that, you could either extend MediaPlayer and wrap all of the above into a custom method (startAtTime?), or create a ProxyElement that wraps a VideoElement and does the above in a custom LoadTrait.  The former is simpler, the latter is more complex but more powerful/flexible/reusable.

              • 4. Re: Whats is the strategy for starting a video in the 5th second ?
                Soluciones1A Level 1

                Thanks a lot for your response.

                 

                ive implemented the logic you describe, the code is here: http://solucionesgen.com/osmf/VideoElementTest.mxml im using flash builder 4 final.

                have two problems.

                 

                1- the SeekingChange event does not exist, only SeekEvent.SEEK_BEGIN and SeekEvent.SEEK_END

                2- after the call to seek (rigth after the second PlayingChangeEvent) nothing  happens.

                 

                I am callingit from outside, then the two SeekEvents are dispatched, but nothing is played, ive chequed the mediaPlayer.currentTime and is in fact 5 secs.

                • 5. Re: Whats is the strategy for starting a video in the 5th second ?
                  Soluciones1A Level 1

                  This is part of the code in case someone else could need it.

                  protected function creationCompleteHandler(event:FlexEvent):void
                              {
                                  canvas = new RegionGateway();
                                  canvas.clipChildren = true;
                                  canvas.x = 0; canvas.y = 0; canvas.backgroundColor = 0x00ff00;               
                                  //canvas.backgroundAlpha = 1.0;
                                  LayoutUtils.setAbsoluteLayout(canvas.metadata, 720, 405);
                                  LayoutUtils.setLayoutAttributes(canvas.metadata, ScaleMode.ZOOM, RegistrationPoint.BOTTOM_LEFT);
                                  sve.addChild(canvas);
                                 
                                  mediaPlayer = new MediaPlayer();
                                  mediaPlayer.muted = true;
                                  mediaPlayer.autoPlay = false;
                                  var path:String = "C:\\video.f4v";
                                  videoElement = new VideoElement
                                      ( new NetLoader()
                                          , new URLResource(new URL("file://"+path))
                                      );
                                  videoElement.gateway = canvas;
                                  mediaPlayer.element = videoElement;
                                  mediaPlayer.addEventListener(MediaPlayerStateChangeEvent.MEDIA_PLAYER_STATE_CHANGE,onMpsc );
                                  mediaPlayer.addEventListener(PlayingChangeEvent.PLAYING_CHANGE,onPlayingChange);
                                  mediaPlayer.play();
                              }
                              private var playingChangeCount:int = 0;
                              private function onPlayingChange(event:Object):void{
                                  trace("onPlayingChange");
                                  playingChangeCount++;
                                  if(playingChangeCount==1){
                                      trace("first playingChange... pausing");
                                      mediaPlayer.pause();
                                  } else if (playingChangeCount==2){
                                      trace("second playingChange... seeking");
                                      mediaPlayer.removeEventListener(PlayingChangeEvent.PLAYING_CHANGE,onPlayingChange);
                                      mediaPlayer.addEventListener(SeekEvent.SEEK_BEGIN,onSeekingChange);
                                      mediaPlayer.addEventListener(SeekEvent.SEEK_END,onSeekingChange);
                                      mediaPlayer.seek(5);
                                  }
                              }
                             
                              private var seekingChangeCount:int = 0;
                              private function onSeekingChange(event:org.osmf.events.SeekEvent):void{
                                  trace("onSeekingChange");
                                  seekingChangeCount++;
                                  if(seekingChangeCount==1){
                                      trace("initiating seek");
                                  } else if (seekingChangeCount==2){
                                      trace("seek finished");
                                      mediaPlayer.muted = false;
                                      mediaPlayer.removeEventListener(SeekEvent.SEEK_BEGIN,onSeekingChange);
                                      mediaPlayer.removeEventListener(SeekEvent.SEEK_END,onSeekingChange);
                                      trace("mediaPlayer.currentTime is:"+ mediaPlayer.currentTime);
                                      mediaPlayer.play();
                                  }
                              }
                             
                              private function onMpsc(event:MediaPlayerStateChangeEvent):void{
                                  trace(event.toString() + event.state);               
                              }
                  • 6. Re: Whats is the strategy for starting a video in the 5th second ?
                    Soluciones1A Level 1

                    Do you think that this startAt(5) is possible using osmf or should i go to the flash.media.video compoenent?

                     

                    thanks in advance.

                    • 7. Re: Whats is the strategy for starting a video in the 5th second ?
                      bringrags Level 4

                      A couple of quick responses:

                      • The seekBegin/seekEnd events are from a previous sprint release (sprint 9, I think), the seekingChange event is the new name.  And looking at your code, it seems you're on a pretty old release.  You might want to get the latest release (or public trunk), since those may have bug fixes that are relevant to your case.
                      • What you're trying to do should be equally easy/hard with OSMF vs. flash.media.Video.  I don't think it's a trivial thing to implement (before we deferred the feature for 1.0, we had an estimate of 3-5 days for implementation).  But definitely let us know if you get it working.