3 Replies Latest reply on Sep 29, 2009 11:56 AM by gloam

    MouseEvent and MediaElement

    pfff... Level 1

      Hi,

       

      is it possible to add  MouseEvent Click on  MediaElement ?

       

      when i use  addEventListener the mouseevent  are not proposed why ?

       

      Thanks in advance

       

      Stef

        • 1. Re: MouseEvent and MediaElementI
          gloam

          I'm new to OSMF, but I'll take a shot at your question:

           

          MouseEvents are generally dispatched by DisplayObject or subclasses in AS3.  MediaElement and subclasses in OSMF do not inherit from DisplayObject, so don't dispatch MouseEvents.

           

          However, any viewable MediaElement (which will have an IViewable trait) uses a DisplayObject to render on screen. So, you can retrieve the DisplayObject used to render the Media Element and put a listener on that.  You might try something like this:

           

          //retrieves the IViewable traint from the media element

          var viewable:IViewable = yourMediaElement.getTrait(MediaTraitType.VIEWABLE) as IViewable;


          //checks to make sure this trait is not null, and if it is not null, gets the display object being used for
          //the view and adds a MouseEvent Listener to it

          if(viewable)

          {

          var displayObject:DisplayObject = viewable.view;

          displayObject.addEventListener(MouseEvent.CLICK, onClick);

          }


          //the handler for the click

          private function onClick(e:Event):void

          {

              trace("You clicked on yourMediaElement");

          }

           

          Hope that helps.

          • 2. Re: MouseEvent and MediaElementI
            Edwin van Rijkom Level 2

            Hi Gloam,

             

            That all sounds right. Wanted to add a couple of subtilities:

             

            DisplayObject does technically not emit mouse or keyboard events: for that, an InteractiveObject is required, which is higher up the chain. In practice both Sprite and MovieClip are InteractiveObjects, and those are likely to be passed as a view. Video and Shape are DisplayObjects, but are not InteractiveObjects, so adding a click handler there wouldn't get the desired result.

             

            We've been thinking about adding an IInteractive trait to address interactivity in a more generic way; but this is still on the drawing board.

             

            Cheers,

            Edwin

            • 3. Re: MouseEvent and MediaElementI
              gloam Level 1

              Oops - thanks Edwin for pointing out that error.  Sorry to Stef for my mistake:  it isn't DisplayObject, but rather InteractiveObject that dispatches Mouse and Keyboard events.  The method I outlined above will only work for something that extends Sprite, MovieClip, or another InteractiveObject subclass.  And the code would have to be written like this:

               

              //retrieves the IViewable traint from the media element

               

              var viewable:IViewable = yourMediaElement.getTrait(MediaTraitType.VIEWABLE) as IViewable;


              //checks to make sure this trait is not null and that the display object being used is a subclass of //InteractiveObject. If these conditions are true, gets the display object being used for the view and adds
              //a MouseEvent Listener to it

               

              if(viewable && viewable.view is InteractiveObject)

              {

              var iObject:InteractiveObject = viewable.view as InteractiveObject;

              iObject.addEventListener(MouseEvent.CLICK, onClick);

              }


              //the handler for the click

               

              private function onClick(e:Event):void

              {

                  trace("You clicked on yourMediaElement");

              }

               

              If the media being rendered is a Video object, you would need to add a Sprite or similar object over the top to capture MouseEvents.  I might be able to help you figure that out if it's what you are trying to do.