/AdobeAIRSDK/frameworks/projects/osmf/src/org/osmf/media/MediaPlayerSprite.as

https://github.com/kibiz0r/FlashRuby · ActionScript · 272 lines · 115 code · 19 blank · 138 comment · 12 complexity · 88712475047714cc15f961ddcb31da71 MD5 · raw file

  1. /*****************************************************
  2. *
  3. * Copyright 2009 Adobe Systems Incorporated. All Rights Reserved.
  4. *
  5. *****************************************************
  6. * The contents of this file are subject to the Mozilla Public License
  7. * Version 1.1 (the "License"); you may not use this file except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS"
  12. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing rights and limitations
  14. * under the License.
  15. *
  16. *
  17. * The Initial Developer of the Original Code is Adobe Systems Incorporated.
  18. * Portions created by Adobe Systems Incorporated are Copyright (C) 2009 Adobe Systems
  19. * Incorporated. All Rights Reserved.
  20. *
  21. *****************************************************/
  22. package org.osmf.media
  23. {
  24. import flash.display.Sprite;
  25. import flash.events.Event;
  26. import org.osmf.containers.MediaContainer;
  27. import org.osmf.events.MediaElementChangeEvent;
  28. import org.osmf.layout.HorizontalAlign;
  29. import org.osmf.layout.LayoutMetadata;
  30. import org.osmf.layout.ScaleMode;
  31. import org.osmf.layout.VerticalAlign;
  32. /**
  33. * MediaPlayerSprite provides MediaPlayer, MediaContainer, and MediaFactory
  34. * capabilities all in one Sprite-based class. It also provides convenience
  35. * methods to generate MediaElements from a resource and set the ScaleMode.
  36. *
  37. * @includeExample MediaPlayerSpriteExample.as -noswf
  38. *
  39. * @langversion 3.0
  40. * @playerversion Flash 10
  41. * @playerversion AIR 1.5
  42. * @productversion OSMF 1.0
  43. **/
  44. public class MediaPlayerSprite extends Sprite
  45. {
  46. /**
  47. * Constructor.
  48. *
  49. * @param mediaPlayer A custom MediaPlayer can be provided. If null, defaults to new MediaPlayer.
  50. * @param mediaContainer A custom MediaContainer can be provided. If null defaults to a new MediaContainer.
  51. * @param mediaFactory A custom MediaFactory can be provided. If null defaults to a new DefaultMediaFactory.
  52. *
  53. * @langversion 3.0
  54. * @playerversion Flash 10
  55. * @playerversion AIR 1.5
  56. * @productversion OSMF 1.0
  57. **/
  58. public function MediaPlayerSprite(mediaPlayer:MediaPlayer = null, mediaContainer:MediaContainer = null, mediaFactory:MediaFactory = null)
  59. {
  60. _mediaPlayer = mediaPlayer ? mediaPlayer : new MediaPlayer();
  61. _mediaFactory = mediaFactory;
  62. _mediaContainer = mediaContainer ? mediaContainer : new MediaContainer();
  63. _mediaPlayer.addEventListener(MediaElementChangeEvent.MEDIA_ELEMENT_CHANGE, onMediaElementChange);
  64. addChild(_mediaContainer);
  65. if (_mediaPlayer.media != null)
  66. {
  67. media = _mediaPlayer.media;
  68. }
  69. }
  70. /**
  71. * Source MediaElement presented by this MediaPlayerSprite.
  72. *
  73. * <p>Setting the element will set it as the media on the mediaPlayer,
  74. * and add it to the media container. Setting this property to null will remove it
  75. * both from the player and container. Existing in properties, such as layout will be
  76. * preserved on media.</p>
  77. *
  78. * @langversion 3.0
  79. * @playerversion Flash 10
  80. * @playerversion AIR 1.5
  81. * @productversion OSMF 1.0
  82. */
  83. public function get media():MediaElement
  84. {
  85. return _media;
  86. }
  87. public function set media(value:MediaElement):void
  88. {
  89. if (_media != value)
  90. {
  91. if (_media && _mediaContainer.containsMediaElement(_media))
  92. {
  93. _mediaContainer.removeMediaElement(_media);
  94. }
  95. _media = value;
  96. if (_media && _media.getMetadata(LayoutMetadata.LAYOUT_NAMESPACE) == null)
  97. {
  98. var layout:LayoutMetadata = new LayoutMetadata();
  99. layout.scaleMode = _scaleMode;
  100. layout.verticalAlign = VerticalAlign.MIDDLE;
  101. layout.horizontalAlign = HorizontalAlign.CENTER;
  102. layout.percentWidth = 100;
  103. layout.percentHeight = 100;
  104. _media.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, layout);
  105. }
  106. _mediaPlayer.media = value;
  107. if (value && !_mediaContainer.containsMediaElement(value) )
  108. {
  109. _mediaContainer.addMediaElement(value);
  110. }
  111. }
  112. }
  113. /**
  114. * The resource corresponding to the media element that is currently
  115. * being presented by this MediaPlayerSprite.
  116. *
  117. * <p>When set, this property uses the MediaFactory to generate a new
  118. * MediaElement, and sets it as the MediaElement on this MediaPlayerSprite.
  119. * If null, it will remove the existing MediaElement and resource from
  120. * the player and container. If the MediaFactory can't create a
  121. * MediaElement from the given resource, it will set the media and
  122. * to null.</p>
  123. *
  124. * @langversion 3.0
  125. * @playerversion Flash 10
  126. * @playerversion AIR 1.5
  127. * @productversion OSMF 1.0
  128. */
  129. public function get resource():MediaResourceBase
  130. {
  131. return _media ? _media.resource : null;
  132. }
  133. public function set resource(value:MediaResourceBase):void
  134. {
  135. media = value ? mediaFactory.createMediaElement(value) : null;
  136. }
  137. /**
  138. * The MediaPlayer that controls this media element.
  139. *
  140. * <p>Defaults to an instance of MediaPlayer. When an element is set
  141. * directly on the MediaPlayer, the media element is propagated to
  142. * the MediaPlayerSprite, and the MediaContainer.</p>
  143. *
  144. * @langversion 3.0
  145. * @playerversion Flash 10
  146. * @playerversion AIR 1.5
  147. * @productversion OSMF 1.0
  148. */
  149. public function get mediaPlayer():MediaPlayer
  150. {
  151. return _mediaPlayer;
  152. }
  153. /**
  154. * The MediaContainer that is used with this class.
  155. *
  156. * <p>Defaults to an instance of MediaContainer. Any media elements
  157. * added or removed through addMediaElement() or removeMediaElement()
  158. * are not set on the MediaPlayer. In order to set the MediaElement,
  159. * use the setter on the MediaPlayerSprite.</p>
  160. *
  161. * @langversion 3.0
  162. * @playerversion Flash 10
  163. * @playerversion AIR 1.5
  164. * @productversion OSMF 1.0
  165. */
  166. public function get mediaContainer():MediaContainer
  167. {
  168. return _mediaContainer;
  169. }
  170. /**
  171. * The MediaFactory that is used with this class.
  172. *
  173. * <p>Defaults to an instance of DefaultMediaFactory. Plugins should
  174. * be loaded through this media factory. Media elements created
  175. * directly through this factory aren't added to the MediaPlayer or
  176. * MediaContainer. To associate a new media element with the
  177. * MediaPlayerSprite, set the media property on this class.</p>
  178. *
  179. * @langversion 3.0
  180. * @playerversion Flash 10
  181. * @playerversion AIR 1.5
  182. * @productversion OSMF 1.0
  183. */
  184. public function get mediaFactory():MediaFactory
  185. {
  186. _mediaFactory = _mediaFactory ? _mediaFactory : new DefaultMediaFactory();
  187. return _mediaFactory;
  188. }
  189. /**
  190. * Defines how content within the MediaPlayerSprite will be laid out.
  191. *
  192. * <p>The default value is <code>letterbox</code>.</p>
  193. *
  194. * <p>Note that by default the MediaContainer sets the layout to be 100%
  195. * width, 100% height, and centered.</p>
  196. *
  197. * @langversion 3.0
  198. * @playerversion Flash 10
  199. * @playerversion AIR 1.5
  200. * @productversion OSMF 1.0
  201. */
  202. public function get scaleMode():String
  203. {
  204. return _scaleMode;
  205. }
  206. public function set scaleMode(value:String):void
  207. {
  208. _scaleMode = value;
  209. if (_media)
  210. {
  211. var layout:LayoutMetadata = _media.getMetadata(LayoutMetadata.LAYOUT_NAMESPACE) as LayoutMetadata;
  212. layout.scaleMode = value;
  213. }
  214. }
  215. /**
  216. * @private
  217. */
  218. override public function set width(value:Number):void
  219. {
  220. _mediaContainer.width = value;
  221. }
  222. /**
  223. * @private
  224. */
  225. override public function set height(value:Number):void
  226. {
  227. _mediaContainer.height = value;
  228. }
  229. /**
  230. * @private
  231. */
  232. override public function get width():Number
  233. {
  234. return _mediaContainer.width;
  235. }
  236. /**
  237. * @private
  238. */
  239. override public function get height():Number
  240. {
  241. return _mediaContainer.height;
  242. }
  243. private function onMediaElementChange(event:MediaElementChangeEvent):void
  244. {
  245. media = _mediaPlayer.media;
  246. }
  247. private var _scaleMode:String = ScaleMode.LETTERBOX;
  248. private var _media:MediaElement;
  249. private var _mediaPlayer:MediaPlayer;
  250. private var _mediaFactory:MediaFactory;
  251. private var _mediaContainer:MediaContainer;
  252. }
  253. }