FMS播放FLV视频
发布端:
注意的就是flv的位置,默认位于application\你的应用程序名称\streams\_definst_。
代码中文件名不加扩展名
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Style>
Application {
font-size:12px;
font-style:normal;
font-weight:normal;
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var netConnection:NetConnection;
private var netStream:NetStream;
private var appServer:String="rtmp://192.168.1.10/meeting";
private function init():void
{
netConnection = new NetConnection();
netConnection.objectEncoding = ObjectEncoding.AMF0;
netConnection.connect(appServer);
netConnection.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
btnPlay.addEventListener(MouseEvent.CLICK,playVideo);
}
private function netStatusHandler(evt:NetStatusEvent):void{
if (evt.info.code == "NetConnection.Connect.Success")
{
Alert.show("连接成功");
}
else
{
Alert.show("连接失败");
}
}
private function playVideo(evt:MouseEvent):void{
var objMetaData:Object = new Object();
objMetaData.onMetaData=onMetaData;
netStream = new NetStream(netConnection);
netStream.client=objMetaData;
netStream.play("war3");
var video:Video = new Video(300,200);
video.x = 0;
video.y = 0;
video.attachNetStream(netStream);
comp.addChild(video);
}
private function onMetaData(data:Object):void {
}
]]>
</mx:Script>
<mx:UIComponent id="comp" width="300" height="200" x="10" y="10" themeColor="#8B8B8B">
</mx:UIComponent>
<mx:Button x="10" y="218" label="发布" id="btnPlay"/>
</mx:Application>
客户端:
注意流名字对应,flex里显示可用Video播放(放在UIComponent中),也可用VideoDisplay播放.
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();" height="432">
<mx:Style>
Application {
font-size:12px;
font-style:normal;
font-weight:normal;
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.*;
private var netConnection:NetConnection;
private var netStream:NetStream;
private var appServer:String="rtmp://192.168.1.10/meeting";
private var playStatus:String = "no play";
private var videoPosition:Number;
private var soundPosition:Number;
private var timer:Timer=new Timer(10);
private function init():void{
netConnection = new NetConnection();
netConnection.objectEncoding = ObjectEncoding.AMF0;
netConnection.connect(appServer);
netConnection.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
netConnection.client=this;
}
private function netStatusHandler(evt:NetStatusEvent):void{
if (evt.info.code != "NetConnection.Connect.Success")
{
Alert.show("连接失败!");
}
}
//播放或暂停
private function btnPlayPause_onClick(evt:MouseEvent):void{
if (playStatus=="no play"){
var objMetaData:Object = new Object();
objMetaData.onMetaData=onMetaData;
netStream=new NetStream(netConnection);
netStream.play("sds");
netStream.addEventListener(NetStatusEvent.NET_STATUS,netStreamStatusHandler);
netStream.client=objMetaData;
var video:Video = new Video(300,200);
video.attachNetStream(netStream);
video2.addChild(video);
video.width=300;
video.height=200;
playStatus="palying";
btnPlayPause.label="暂停";
}
else if (playStatus=="palying"){
netStream.pause();
playStatus="pause";
btnPlayPause.label="播放";
}
else{
netStream.resume();
playStatus="palying";
btnPlayPause.label="暂停";
}
}
//成功接收到流数据响应函数,存储流的各项属性
private function onMetaData(data:Object):void {
hs_video.maximum = data.duration;
timer.addEventListener(TimerEvent.TIMER,onTimer);
timer.start();
}
private function netStreamStatusHandler(evt:NetStatusEvent):void{
trace(evt.info.code);
if (evt.info.code=="NetStream.Play.Stop"){
netStream.seek(0);
}
}
//轮循事件中更新播放进度
private function onTimer(evt:TimerEvent):void{
hs_video.value = netStream.time;
}
//播放进度控制
private function video_thumbChanges(event:SliderEvent):void{
timer.stop();
videoPosition = hs_video.value;
netStream.seek(videoPosition);
timer.start();
}
//声音音量控制
private function sound_thumbChanges(event:SliderEvent):void{
soundPosition = hs_sound.value;
var stf:SoundTransform=new SoundTransform();
stf.volume = soundPosition;
netStream.soundTransform = stf;
}
]]>
</mx:Script>
<mx:VideoDisplay id="video2" width="300" height="200" x="10" y="10">
</mx:VideoDisplay>
<mx:Button x="10" y="218" label="播放" id="btnPlayPause" click="btnPlayPause_onClick(event);"/>
<mx:HSlider id="hs_video" x="10" y="250" width="300" minimum="0" value="0" liveDragging="true" snapInterval="0.1" tickInterval="2" change="video_thumbChanges(event)"/>
<mx:HSlider id="hs_sound" x="10" y="270" width="52" minimum="0" maximum="1" snapInterval="0.01" tickInterval="0.1" liveDragging="true" value="1" change="sound_thumbChanges(event)"/>
<mx:Label x="70" y="220" id="lblTime"/>
</mx:Application>