4 Replies Latest reply on Nov 17, 2009 10:43 AM by tyjohnston

    How to use MediaFactory?

    tyjohnston

      I would like to use the MediaFactory class to injest a URL resource and spit out the proper MediaElement.  In the specifications for the MediaPlayer class, there was an example that looked like so:

       

      var resource:URLResource = new URLResource("http://example.com/video.flv");
      var mediaElement:MediaElement = MediaFactory.createElement(resource);
      var player:MediaPlayer = new MediaPlayer();

      player.autoPlay = true;

      mediaElement.resource = resource;
      player.media = mediaElement;


      However, this example is outdated.  So I am using:

       

      var mf:MediaFactory = new MediaFactory()
      var resource:URLResource = new URLResource( new URL( myURL ) );
      var element:MediaElement = mf.createMediaElement( resource );

      I get the following error when I call MediaPlayer.play():

       

      "Error: MediaPlayer - attempt to use a trait not on the media"

       

      How do I add traits?  And what is the variable being passed through the constructer? (handlerResolver:IMediaResourceHandlerResolver=null)

      How do I create a handlerResolver?

       

      What is the proper way to dynamically create a MediaElement by only looking at the url?

        • 1. Re: How to use MediaFactory?
          Edwin van Rijkom

          MediaFactory does not know about any sort of media type when instantiated. To have it map resource types to media elements, it needs to learn about media types. The snippet below registers the VideoElement, for example.

           

          var netLoader:NetLoader = new NetLoader();
          
          var factory:MediaFactory = new MediaFactory();
          factory.addMediaInfo
               ( new MediaInfo
                    ( "org.osmf.video"
                    , netLoader
                    , function():MediaElement
                         {
                              return new VideoElement(netLoader);
                         }
                    )
               );
          
          var resource:URLResource = new URLResource(new URL("http://example.com/video.flv"));
          var mediaElement:MediaElement = factory.createMediaElement(resource);
          

           

          Setting mediaElement.resource after that is not necessary, for the factory will have it assigned it already.

           

          Cheers,

          Edwin

          • 2. Re: How to use MediaFactory?
            tyjohnston Level 1

            Ok, so I've done the following to be able to load either a Video, Audio or Image without explicitly setting the MediaElement type.

             

            var netLoader:NetLoader = new NetLoader();
            var soundLoader:SoundLoader = new SoundLoader();
            var imageLoader:ImageLoader = new ImageLoader();
             
            _factory = new MediaFactory();
            _factory.addMediaInfo
                 ( new MediaInfo
                      ( "org.osmf.video"
                      , netLoader
                      , function():MediaElement
                           {
                                return new VideoElement(netLoader);
                           }
                      )
                 );
            _factory.addMediaInfo
                      ( new MediaInfo
                      ( "org.osmf.audio"
                      , soundLoader
                      , function():MediaElement
                           {
                                return new AudioElement(soundLoader);
                           }
                      )
                 );
                 
            _factory.addMediaInfo
                      ( new MediaInfo
                      ( "org.osmf.image"
                      , imageLoader
                      , function():MediaElement
                           {
                                return new ImageElement(imageLoader);
                           }
                      )
                 );
            

             

            When I pass an mp3 through, I get the "trait not supported" error again, by calling Mediaplayer.play().  When I called play() when loading an flv, it worked fine.  Shouldn't the MediaFactory map the mp3 to the AudioElement?

            • 3. Re: How to use MediaFactory?
              bringrags Level 4

              Does your URLResource have the ".mp3" extension?  If not, then the SoundLoader won't know that it can handle the resource.  See this forum post for how to set metadata on the resource so that the MediaFactory can determine its type.

               

              Also, it's possible you're calling play() before the media is actually able to be played.  Try setting the MediaPlayer.autoPlay property, as that will cause the media to play as soon as it's ready.  (If the media doesn't auto-play, it's probably due to the above metadat issue.)

              • 4. Re: How to use MediaFactory?
                tyjohnston Level 1

                Thank you for your replies.  Like Brian said, I believe I was calling play() before anythign was actually loaded.  Smart player   Thanks for the help thus far!