2 Replies Latest reply on Aug 11, 2010 10:32 AM by rshin

    Get audio/song duration before completely loaded


      Is it possible to somehow get aprox song duration (length) before song is completely loaded?

      mediaPlayer.duration is inacurate in first several seconds of playing, until entire song is loaded and that produces many wrong calculations in my player (playhead marker expecially) during that time. Is only way to dig that from ID3?


      I am trying to make player same as on djdownload.com, and need this fast loading feature they have. Anyone have some suggestions?

        • 1. Re: Get audio/song duration before completely loaded

          I'm not sure about audio but I have mine set up to get the video duration (before being fully loaded) by adding a listener to mediaPlayer.addEventListener(MediaPlayerCapabilityChangeEvent.CAN_SEEK_CHANGE, onCanSeek);


          Within the onCanSeek method I'm able to get the mediaPlayer.duration value.


          Hope this helps.

          • 2. Re: Get audio/song duration before completely loaded
            rshin Level 3

            Have you tried "pre-load" before you play and get the value?


            This was part of examples previously demonstrated in the previous version of OSMF example player. (There might be a few glitch if this is used in a complex media element)


            After media element creation, you could call a function like this.

            preload(audioElement, false);


            private static function preload(mediaElement:MediaElement, doPlayPause:Boolean):void
                        var loadTrait:LoadTrait = mediaElement.getTrait(MediaTraitType.LOAD) as LoadTrait;
                        if (doPlayPause)
                            loadTrait.addEventListener(LoadEvent.LOAD_STATE_CHANGE, onLoadStateChange);
                        function onLoadStateChange(event:LoadEvent):void
                            if (event.loadState == LoadState.READY)
                                var playTrait:PlayTrait = mediaElement.getTrait(MediaTraitType.PLAY) as PlayTrait;
                                loadTrait.removeEventListener(LoadEvent.LOAD_STATE_CHANGE, onLoadStateChange);