我将在下面的例子演示中,使用Red5作为流媒体服务器,使用到读取XML配置文件技术、SharedObject传参和事件传递机制(注:事件本身是可以存储和传递参数的)。
读取配置文件,包括两个值:流媒体服务连接地址和初始化的视频名称。
<?xml version="1.0"?>
<videoConfig>
<item>
<rtmpUrl>rtmp://localhost/oflaDemo/</rtmpUrl>
<filmName>IronMan.flv</filmName>
</item>
</videoConfig>
<videoConfig>
<item>
<rtmpUrl>rtmp://localhost/oflaDemo/</rtmpUrl>
<filmName>IronMan.flv</filmName>
</item>
</videoConfig>
自己写的简单的事件类VideoEvent,主要用来发送事件和监听事件。
事件类
package
{
import flash.events.EventDispatcher;
public class VideoEvent extends EventDispatcher
{
// 静态常量,定义事件类型
public static const VidoPlay:String="VideoPlay";
// 静态实例
private static var _instance:VideoEvent;
public static function getInstance():VideoEvent
{
if(_instance==null)
_instance=new VideoEvent();
return _instance;
}
}
}
package
{
import flash.events.EventDispatcher;
public class VideoEvent extends EventDispatcher
{
// 静态常量,定义事件类型
public static const VidoPlay:String="VideoPlay";
// 静态实例
private static var _instance:VideoEvent;
public static function getInstance():VideoEvent
{
if(_instance==null)
_instance=new VideoEvent();
return _instance;
}
}
}
使用VideoPlayer控件的主MXML应用程序代码。首先是读取配置文件的数据,并初始化VideoPlayer控件。
VideoPlayer控件应用
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo" width="500" height="500"
applicationComplete="init()"
>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:HTTPService id="myService" url="videoConfig.xml" result="resultHandler(event)"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
//定义视频播放事件监听对象
public var instance:VideoEvent=VideoEvent.getInstance();
private var filmSource:String="";//IronMan.flv
private function init():void
{
//发送读取配置的请求
myService.send();
//定义视频播放事件监听
instance.addEventListener("VideoPlay",playVideoHandler);
}
//视频监听的处理
private function playVideoHandler(event:Event):void
{
var myVideo:SharedObject;
//将播放头置于视频开始处
myVideo=SharedObject.getLocal("videoCookie");
var vName:String=myVideo.data.vName;
//播放选中的视频
film.source=filmSource+vName;
}
private function changeSource():void
{
var myVideo:SharedObject;
//将播放头置于视频开始处
myVideo=SharedObject.getLocal("videoCookie");
//将视频的文件名称,存放到共享文件里
myVideo.data.vName="DarkKnight.flv";
//一定要先存放VCODE到共享对象里,再分发事件
instance.dispatchEvent(new Event("VideoPlay"));
}
//读取配置文件
private function resultHandler(event:ResultEvent):void
{
//获取流媒体服务器 地址
filmSource=event.result.videoConfig.item.rtmpUrl;
//获取流媒体文件名
var filmName:String=event.result.videoConfig.item.filmName;
//获取流媒体完整路径
film.source=filmSource+filmName;
}
]]>
</fx:Script>
<s:VideoPlayer width="396" height="294" id="film"/>
<s:Button label="更换播放源" buttonDown="changeSource()" x="8" y="301"/>
</s:Application>
**注:使用本实例,需要安装Red5并启动服务。
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo" width="500" height="500"
applicationComplete="init()"
>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:HTTPService id="myService" url="videoConfig.xml" result="resultHandler(event)"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
//定义视频播放事件监听对象
public var instance:VideoEvent=VideoEvent.getInstance();
private var filmSource:String="";//IronMan.flv
private function init():void
{
//发送读取配置的请求
myService.send();
//定义视频播放事件监听
instance.addEventListener("VideoPlay",playVideoHandler);
}
//视频监听的处理
private function playVideoHandler(event:Event):void
{
var myVideo:SharedObject;
//将播放头置于视频开始处
myVideo=SharedObject.getLocal("videoCookie");
var vName:String=myVideo.data.vName;
//播放选中的视频
film.source=filmSource+vName;
}
private function changeSource():void
{
var myVideo:SharedObject;
//将播放头置于视频开始处
myVideo=SharedObject.getLocal("videoCookie");
//将视频的文件名称,存放到共享文件里
myVideo.data.vName="DarkKnight.flv";
//一定要先存放VCODE到共享对象里,再分发事件
instance.dispatchEvent(new Event("VideoPlay"));
}
//读取配置文件
private function resultHandler(event:ResultEvent):void
{
//获取流媒体服务器 地址
filmSource=event.result.videoConfig.item.rtmpUrl;
//获取流媒体文件名
var filmName:String=event.result.videoConfig.item.filmName;
//获取流媒体完整路径
film.source=filmSource+filmName;
}
]]>
</fx:Script>
<s:VideoPlayer width="396" height="294" id="film"/>
<s:Button label="更换播放源" buttonDown="changeSource()" x="8" y="301"/>
</s:Application>