如何在 ActionScript 3/Flex 中缩放 OSMF 播放器
我正在尝试使用 Flex 4 中的开源媒体框架创建一个简单的视频播放器 SWF。我想让它根据用户输入的视频尺寸动态缩放。我正在按照 Adobe 帮助网站,但视频似乎无法正确缩放。根据大小,有时视频播放的大小会大于网页上分配的空间,有时会小于网页上分配的空间。我能够让它正常工作的唯一方法是包含一个对宽度和高度进行硬编码的 SWF 元数据标记,但如果我想让播放器动态调整大小,我就无法使用它。我的代码是:
package {
import flash.display.Sprite;
import flash.events.Event;
import org.osmf.media.MediaElement;
import org.osmf.media.MediaPlayer;
import org.osmf.media.URLResource;
import org.osmf.containers.MediaContainer;
import org.osmf.elements.VideoElement;
import org.osmf.layout.LayoutMetadata;
public class GalleryVideoPlayer extends Sprite {
private var videoElement:VideoElement;
private var mediaPlayer:MediaPlayer;
private var mediaContainer:MediaContainer;
private var flashVars:Object;
public function GalleryVideoPlayer() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
flashVars = loaderInfo.parameters;
mediaPlayer = new MediaPlayer();
videoElement = new VideoElement(new URLResource(flashVars.file));
mediaContainer = new MediaContainer();
var layoutMetadata:LayoutMetadata = new LayoutMetadata();
layoutMetadata.width = Number(flashVars.width);
layoutMetadata.height = Number(flashVars.height);
videoElement.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, layoutMetadata);
mediaPlayer.media = videoElement;
mediaContainer.addMediaElement(videoElement);
addChild(mediaContainer);
}
}}
I am trying to create a simple video player SWF using the open source media framework in Flex 4. I want to make it dynamically scale based on the dimensions of the video, input by the user. I am following the directions on the Adobe help site, but the video does not seem to scale properly. Depending on the size, sometimes videos play larger than the space allotted on the webpage, and sometimes smaller. The only way I have been able to get it to work properly is by including a SWF metadata tag hardcoding the width and height, but I can't use that if I want to make the player dynamically sized. My code is :
package {
import flash.display.Sprite;
import flash.events.Event;
import org.osmf.media.MediaElement;
import org.osmf.media.MediaPlayer;
import org.osmf.media.URLResource;
import org.osmf.containers.MediaContainer;
import org.osmf.elements.VideoElement;
import org.osmf.layout.LayoutMetadata;
public class GalleryVideoPlayer extends Sprite {
private var videoElement:VideoElement;
private var mediaPlayer:MediaPlayer;
private var mediaContainer:MediaContainer;
private var flashVars:Object;
public function GalleryVideoPlayer() {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
flashVars = loaderInfo.parameters;
mediaPlayer = new MediaPlayer();
videoElement = new VideoElement(new URLResource(flashVars.file));
mediaContainer = new MediaContainer();
var layoutMetadata:LayoutMetadata = new LayoutMetadata();
layoutMetadata.width = Number(flashVars.width);
layoutMetadata.height = Number(flashVars.height);
videoElement.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, layoutMetadata);
mediaPlayer.media = videoElement;
mediaContainer.addMediaElement(videoElement);
addChild(mediaContainer);
}
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 flex 不太了解,但我使用了一个名为 StrobeMediaPlayback 的 OSMF 播放器并将其嵌入到 html 页面中。有一个 JavaScript 桥,您可以使用 ActionScript 中的ExternalInterface.call() 函数(在播放器代码中)调用它。
因此,我编写了一个 JavaScript 函数来调整 swf 的大小(可以使用 jquery 轻松完成),并在视频需要缩放时从 actionScript 调用它。
看看这是否对您有帮助。
I don't know much about flex but i've used an OSMF player called the StrobeMediaPlayback and embedded it onto a html page. There's a javascript bridge which you can call using the ExternalInterface.call() function in actionscript (within the player code).
So I wrote a javascript function to resize the swf (can be done easily using jquery) and called it from actionScript whenever the video had to scale.
See if this helps you.