2 Replies Latest reply on Mar 17, 2010 7:38 PM by lisamarienyc

    FLV not firing MediaPlayerStateChangeEvent (was: Traits for Audio Elements?)

    lisamarienyc

      I'm testing this very basic player with various element types; JPG and MP3 work perfectly, but the FLV does nothing. The MediaPlayerStateChangeEvent never fires. Is this a bug, or is there a problem with my implementation? I am certain that I'm using sprint 10.

       

      Current code, with comments...

       

      package
      {
          import flash.display.Sprite;
         
          import org.osmf.containers.MediaContainer;
          import org.osmf.media.DefaultMediaFactory;
          import org.osmf.media.MediaElement;
          import org.osmf.media.MediaPlayer;
          import org.osmf.media.URLResource;
          import org.osmf.utils.URL;
          import org.osmf.traits.*;
          import flash.display.Graphics;
          import flash.display.Sprite;
          import flash.events.MouseEvent;
             
          import org.osmf.containers.*;
          import org.osmf.elements.*;
          import org.osmf.events.*;
          import org.osmf.layout.*;
          import org.osmf.media.*;
          import org.osmf.metadata.*;
          import org.osmf.net.*;
          import org.osmf.net.dvr.*;
          import org.osmf.net.httpstreaming.*;
          import org.osmf.net.rtmpstreaming.*;
          import org.osmf.traits.*;
          import org.osmf.utils.*;

          /**
           * The metadata sets the SWF size to match that of the video.
          **/
          [SWF(width="640", height="480")]
          public class HelloPlayButton extends Sprite {

              public function HelloPlayButton() {
                  // Create the container class that displays the media.
                  var container:MediaContainer = new MediaContainer();
                  addChild(container);
                 
                  // Create URL
                  //var mediaURL:URL = new URL("../videos/S0004_VP6_1Mbps.flv");
                  var mediaURL:URL = new URL("../images/swing.jpg");
                  //var mediaURL:URL = new URL("../audio/train_1500.mp3");
      //**DOCS SAY TO ASSIGN STRING INSTEAD OF URL, BUT THIS THROWS AN ERROR, URL ABOVE DOES NOT
                  //var mediaURL:String = "../videos/S0004_VP6_1Mbps.flv";

                  // Create the resource to play
                  var resource:URLResource = new URLResource(mediaURL);
                 
                  // Create a mediafactory instance
                  var mediaFactory:DefaultMediaFactory = new DefaultMediaFactory();
                 
                  // Create the MediaElement and add it to our container class.
                  var myMediaElement:MediaElement =  mediaFactory.createMediaElement(resource);
                  container.addMediaElement(myMediaElement);
                 
                  // Create a media player
                  var mediaPlayer:MediaPlayer = new MediaPlayer();
                  // Stop from autoplaying
                  mediaPlayer.autoPlay = false;
                  // Then assign the media
                  mediaPlayer.media = myMediaElement;

                  var playButton:Sprite = constructPlayButton();
                  addChild(playButton);
                  playButton.visible = false;
                 
                  // Add event listeners
                  playButton.addEventListener(MouseEvent.CLICK, function():void {mediaPlayer.play();});
                  mediaPlayer.addEventListener(PlayEvent.PLAY_STATE_CHANGE, function():void {playButton.visible = !mediaPlayer.playing; });
         
                  mediaPlayer.addEventListener(MediaPlayerStateChangeEvent.MEDIA_PLAYER_STATE_CHANGE, mediaPlayerStateChanged);

                  function mediaPlayerStateChanged(evt:MediaPlayerStateChangeEvent):void {
      //***THIS DOESN'T FIRE FOR FLV, ONLY MP3 AND JPG
                          if (evt.state==MediaPlayerState.READY) {
                              trace("MediaPlayerState -- READY!");
                              registerTraits();
                          }
                  }
                 
                  function registerTraits() {
                      trace("register traits");
                      if (myMediaElement.hasTrait(MediaTraitType.LOAD)) {
                          var loadable:LoadTrait = myMediaElement.getTrait(MediaTraitType.LOAD) as LoadTrait;
                          trace("This mediaElement is loadable!");
                      }
                      if (myMediaElement.hasTrait(MediaTraitType.PLAY)) {
                          var playable:PlayTrait = myMediaElement.getTrait(MediaTraitType.PLAY) as PlayTrait;
                          trace("This mediaElement is playable!");
                          addChild(playButton);
                          playButton.visible=true;
                      }
                      if (myMediaElement.hasTrait(MediaTraitType.AUDIO)) {
                          var audio:AudioTrait = myMediaElement.getTrait(MediaTraitType.AUDIO) as AudioTrait;
                          trace("This mediaElement has an audio track!");
                          addChild(playButton);
                          playButton.visible=true;
                      }
      //**IF I USE THIS SWITCH STATEMENT INSTEAD OF BASING ON TRAITS (ABOVE)
      //**I GET A COMPILER ERROR
      //**Error #1065: Variable org.osmf.elements::VideoElement is not defined.

                          /*switch (true) {
                              case myMediaElement is VideoElement :
                              trace("This is a VideoElement");
                              playButton.visible=true;
                          break;
                              case mediaPlayer.media is ImageElement :
                          break;
                              case mediaPlayer.media is AudioElement :
                              playButton.visible=true;
                          break;
                              case mediaPlayer.media is SWFElement :
                              playButton.visible=true;
                          break;
                          }*/
                  };
                         
                   function constructPlayButton():Sprite {
                  var size = 100;
                      var x = 330;
                      var y = 225;
                      var f = 1.2;
                      var result:Sprite = new Sprite();
                  var buttonArt:Graphics = result.graphics;
                  buttonArt.lineStyle(1, 0, 0.5);
                  buttonArt.beginFill(0xA0A0A0, 0.5);
                  buttonArt.moveTo(x - size / f, y - size);
                  buttonArt.lineTo(x + size / f, y);
                  buttonArt.lineTo(x - size / f, y + size);
                  buttonArt.lineTo(x - size / f, y - size);
                  buttonArt.endFill();
                  return result;
              }
                 
              }
          }
      }

       

      TIA,

      // Lisa