4 Replies Latest reply on Mar 17, 2010 9:02 AM by bringrags

    Setting source with a MediaContainer

    Randy Troppmann Level 2

      I agree with Jesse Warden in another thread where he complained about the removal of MediaPlayerSprite. That being said I was able to easily change my player to work with Sprint 10. The HelloWorld example is helpful, but the video source needs to be dynamic. As a sanity check, can you tell me if this is a good approach to setting the video source (haven't actually compiled this pasted code):

       

      package
      {
           import flash.display.Sprite;
           
           import org.osmf.containers.MediaContainer;
           import org.osmf.elements.VideoElement;
           import org.osmf.media.MediaPlayer;
           import org.osmf.media.URLResource;
      
           public class HelloWorld extends Sprite
           {
                private var _source:String;
                private var container:MediaContainer;
                private var mediaPlayer:MediaPlayer;
                
                public function HelloWorld()
                {
                      container = MediaContainer();
                     addChild(container);               
                     var mediaPlayer:MediaPlayer = new MediaPlayer();
                }
                
                public function get source():String
                {
                     return _source;
                }
                
                public function set source(value:String):void
                {
                     _source = value;
                     var resource:URLResource = new URLResource(source);
                     if (videoElement) container.removeMediaElement(videoElement);
                     container.addMediaElement(videoElement);
                     mediaPlayer.media = videoElement;               
                }
           }
      }
      
      
        • 1. Re: Setting source with a MediaContainer
          Randy Troppmann Level 2

          Oops. I missed the line in the setter where videoElement gets instantiated. How about this:

           

           

          package
          {
               import flash.display.Sprite;
               
               import org.osmf.containers.MediaContainer;
               import org.osmf.elements.VideoElement;
               import org.osmf.media.MediaPlayer;
               import org.osmf.media.URLResource;
          
               public class HelloWorld extends Sprite
               {
                    private var _source:String;
                    private var container:MediaContainer;
                    private var mediaPlayer:MediaPlayer;
                    
                    public function HelloWorld()
                    {
                          container = MediaContainer();
                         addChild(container);               
                         var mediaPlayer:MediaPlayer = new MediaPlayer();
                    }
                    
                    public function get source():String
                    {
                         return _source;
                    }
                    
                    public function set source(value:String):void
                    {
                         _source = value;
                         var resource:URLResource = new URLResource(source);
                         if (videoElement) container.removeMediaElement(videoElement);
                         videoElement = new VideoElement(resource);
                         container.addMediaElement(videoElement);
                         mediaPlayer.media = videoElement;               
                    }
               }
          }
          
          
          • 2. Re: Setting source with a MediaContainer
            bringrags Level 4

            I think you could just replace the resource directly on the VideoElement:

             

             

            public function set source(value:String):void
            {
                 _source = value;
                 if (videoElement == null)
                 {
                      videoElement = new VideoElement();
                 }
                 videoElement.resource = new URLResource(source);
                 mediaPlayer.media = videoElement;              
            }
            • 3. Re: Setting source with a MediaContainer
              Randy Troppmann Level 2

              That throws an RTE when the setter gets hit a second time:

               

              ArgumentError: An instance of this trait class has already been added to this MediaElement

              at org.osmf.media::MediaElement/setLocalTrait()[/Users/randy/Documents/NAIT-projects/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/media/MediaElement.as:642]

              at org.osmf.media::MediaElement/addTrait()[/Users/randy/Documents/NAIT-projects/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/media/MediaElement.as:415]

              at org.osmf.elements::VideoElement/finishLoad()[/Users/randy/Documents/NAIT-projects/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/elements/VideoElement.as:403]

              at org.osmf.elements::VideoElement/processReadyState()[/Users/randy/Documents/NAIT-projects/ D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/elements/VideoElement.as:314]

              at org.osmf.media::LoadableElementBase/onLoadStateChange()[/Users/randy/Documents/NAIT-proje cts/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/media/LoadableElementBase.as:167]

              at flash.events::EventDispatcher/dispatchEventFunction()

              at flash.events::EventDispatcher/dispatchEvent()

              at org.osmf.traits::LoadTrait/loadStateChangeEnd()[/Users/randy/Documents/NAIT-projects/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/traits/LoadTrait.as:414]

              at org.osmf.traits::LoadTrait/setLoadState()[/Users/randy/Documents/NAIT-projects/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/traits/LoadTrait.as:270]

              at org.osmf.traits::LoadTrait/onLoadStateChange()[/Users/randy/Documents/NAIT-projects/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/traits/LoadTrait.as:421]

              at flash.events::EventDispatcher/dispatchEventFunction()

              at flash.events::EventDispatcher/dispatchEvent()

              at org.osmf.traits::LoaderBase/updateLoadTrait()[/Users/randy/Documents/NAIT-projects/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/traits/LoaderBase.as:208]

              at org.osmf.net::NetLoader/finishLoading()[/Users/randy/Documents/NAIT-projects/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/net/NetLoader.as:277]

              at org.osmf.net::NetLoader/onCreationComplete()[/Users/randy/Documents/NAIT-projects/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/net/NetLoader.as:302]

              at flash.events::EventDispatcher/dispatchEventFunction()

              at flash.events::EventDispatcher/dispatchEvent()

              at org.osmf.net:NetConnectionFactory/create/org.osmf.net:onConnected()[/Users/randy/Document s/NAIT-projects/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/net/NetConnectionFactory.as:204]

              at flash.events::EventDispatcher/dispatchEventFunction()

              at flash.events::EventDispatcher/dispatchEvent()

              at NetNegotiator/onNetStatus()[/Users/randy/Documents/NAIT-projects/D3 video player OSMF/D3 MediaPlayer OSMF/src/org/osmf/net/NetNegotiator.as:212]

              • 4. Re: Setting source with a MediaContainer
                bringrags Level 4

                This is a bug, just fixed it in public trunk.