第一次接触Flash编程,左抄右抄,终于搞出这么个东西。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="VideoPlay()" width="650" height="500">
<mx:Style source="xm3.css"/>
<mx:Script>
<![CDATA[
//import JimmyEvent.ValueChangeEvent;
import flash.display.Sprite;
import flash.events.*;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import mx.controls.Button;
private var videoURL:String = "某s自行车.flv";
//private var videoURL:String = "flvVideos/xyj.flv";
private var connection:NetConnection;
private var stream:NetStream;
private var video:Video = new Video();
private var bfxh:String;
public function VideoPlay():void {
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
//connection.connect("rtmp://localhost:8025/");
connection.connect(null);
}
private function netStatusHandler(event:NetStatusEvent):void {
bfxh = event.info.code;
message.text=bfxh;
switch (bfxh) {
case "NetConnection.Connect.Success":
trace("连接成功!");
connectStream();
//gx(Event.ENTER_FRAME);
addEventListener(Event.ENTER_FRAME, gx);
//bar_width();
break;
case "NetStream.Play.StreamNotFound":
trace("Unable to locate video: " + videoURL);
break;
}
}
private function connectStream():void {
//var stream:NetStream = new NetStream(connection);
stream=new NetStream(connection);
//stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
//stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
video.width = sprct.width;
video.height = sprct.height;
video.attachNetStream(stream);
stream.play(encodeURI(videoURL));
sprct.addChild(video);
_client.onMetaData = onMetaData;
stream.client=_client;
//btn_pause.onRelease = function() {
// stream.pause();
//};
}
//音量控制
private var yl:Number = 0.5;
private var nsyl:SoundTransform = new SoundTransform();
private var fw:Rectangle = new Rectangle(187, 326, 50, 0); //拖动范围
//视频控制
private var bf_percent:int=0;
private var xz_percent:int=0;
private var _duration:Number=0;
private var vfw:Rectangle=new Rectangle(314,326,175,0);
private var _client:Object = new Object();
protected function onMetaData(data:Object):void
{
_duration=data.duration;
}
protected function gx(event:Event):void
{
if (stream.bytesLoaded > 0){
//加载进度
xz_percent = stream.bytesLoaded / stream.bytesTotal * 100;
jzt.scaleX=xz_percent / 100;
}
if (_duration > 0 ){
//播放进度
if(stream.time > 0){
bf_percent = stream.time / _duration * 100;
bft.scaleX=bf_percent /100*176;
}
}
if (bfxh == "NetStream.Play.Stop"){
//播放完毕时的设置
bf_percent = 0;
bft.scaleX=0;
//stream.close();
stream.pause();
stream.seek(0); //将播放头置于视频开始处
//play_btn.visible = true;
//pause_btn.visible = false;
}
//文本显示内容
var bfTime:String=Math.floor(stream.time / 60) + ":" + Math.round(stream.time % 60);
var totleTime:String=Math.round(_duration / 60) + ":" + Math.round(_duration % 60);
time_lbl.text=bfTime+"/"+totleTime;
//音量控制
yl = (ylhk_mc.x - 187) / 50;
ylt_mc.scaleX = yl;
nsyl.volume = yl;
stream.soundTransform = nsyl;
//音量滑块拖动控制
ylhk_mc.addEventListener(MouseEvent.MOUSE_DOWN, silderSoundMouseDown);
ylhk_mc.addEventListener(MouseEvent.MOUSE_UP, stageSoundMouseUpHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, stageSoundMouseUpHandler);
//视频滑块拖动控制
this.jzt.addEventListener(MouseEvent.MOUSE_OVER, MouseOverHandler);
this.jzt.addEventListener(MouseEvent.MOUSE_OUT, MouseOutHandler);
this.bft.addEventListener(MouseEvent.MOUSE_OVER, MouseOverHandler);
this.bft.addEventListener(MouseEvent.MOUSE_OUT, MouseOutHandler);
//videohk_mc.addEventListener(MouseEvent.MOUSE_DOWN, silderVideoMouseDown);
//videohk_mc.addEventListener(MouseEvent.MOUSE_UP, stageVideoMouseUpHandler);
//stage.addEventListener(MouseEvent.MOUSE_UP, stageVideoMouseUpHandler);
}
//播放
protected function PlayVideo(event:MouseEvent):void
{
//btn_start.label="ok";
//addEventListener(MouseEvent.CLICK, jt);
stream.resume();
//stream.play();
}
//暂停
protected function PauseVideo(event:MouseEvent):void
{
//pause.label="ok";
stream.pause()
}
//停止
protected function StopVideo(event:MouseEvent):void
{
stream.seek(0);
stream.pause();
}
//切换光标为手形
private function MouseOverHandler(e:MouseEvent):void {
Mouse.cursor=MouseCursor.BUTTON;
}
//切换光标为系统光标
private function MouseOutHandler(e:MouseEvent):void {
Mouse.cursor=MouseCursor.AUTO;
}
//在背景条上点击时,滑块直接跳到该位置
private function jztMouseDownHandler(e:MouseEvent):void {
//bft.scaleX=(mouseX- 314)/175;
bft.scaleX=(mouseX- jzt.x)/jzt.width;
//message.text=mouseX.toString()+":"+(mouseX- 314)/175;
//RaiseEvent();
stream.pause();
stream.seek((mouseX- jzt.x)/jzt.width* _duration);
stream.resume();
}
//视频音量拖动
private function silderSoundMouseDown(event:MouseEvent):void {
ylhk_mc.startDrag(false, fw);
}
private function stageSoundMouseUpHandler(event:MouseEvent):void {
ylhk_mc.stopDrag();
}
//是否是全屏
private var isFullScreen:Boolean = false;
//切換全屏顯示
private function display():void{
if(!isFullScreen)
{ stage.fullScreenSourceRect = new Rectangle(sprct.x,sprct.y,sprct.width,sprct.height); stage.displayState =StageDisplayState.FULL_SCREEN; isFullScreen = true; }
else{
stage.displayState = StageDisplayState.NORMAL; isFullScreen = false; } }
//全屏
protected function fullSc_clickHandler(event:MouseEvent):void
{
if(stage.displayState=="fullScreen")
{
stage.displayState="normal";
video.height = sprct.height;
video.width=sprct.width;
stage.addEventListener(FullScreenEvent.FULL_SCREEN, handleAppFullScreen, false, 0, true);
fullSc.label="全屏";
}
else
{
stage.displayState="fullScreen";
video.height = stage.height-50;
video.width=stage.width-50;
fullSc.label="退出";
}
}
private function handleAppFullScreen(event:FullScreenEvent):void
{
if(!event.fullScreen)
{
video.height = sprct.height;
video.width=sprct.width;
}
}
//错误处理
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function asyncErrorHandler(event:AsyncErrorEvent):void {
// ignore AsyncErrorEvent events.
}
]]>
</mx:Script>
<mx:VideoDisplay id="sprct" x="32" y="23" width="503" height="292" doubleClickEnabled="true" doubleClick="fullSc_clickHandler(event)" />
<mx:Button label="开始" id="playBtn" x="31" y="323" width="41" click="PlayVideo(event)"/>
<mx:Button label="暂停" id="pauseBtn" x="73" y="323" width="41" click="PauseVideo(event)"/>
<mx:Button label="停止" id="stopBtn" x="115" y="323" width="41" click="StopVideo(event)"/>
<mx:Label x="159" y="327" text="音量" height="17"/>
<mx:Canvas x="187" y="331" width="50" height="10" backgroundColor="#F1EBEB" id="ylt_mc"></mx:Canvas>
<mx:Canvas x="196" y="326" width="5" height="15" backgroundColor="#FFFF00" id="ylhk_mc"></mx:Canvas>
<mx:Label x="251" y="327" width="60" id="time_lbl" height="17"/>
<mx:Canvas id="jzt" x="314" y="331" width="175" height="10" backgroundColor="#F1EBEB" click="jztMouseDownHandler(event)">
</mx:Canvas>
<mx:Canvas id="bft" backgroundColor="#FF0000" height="10" y="331" x="314" width="1" click="jztMouseDownHandler(event)"></mx:Canvas>
<mx:Button id="fullSc" x="491" y="323" width="44" label="全屏" click="display()"/>
<mx:Label x="251" y="347" width="300" id="message" height="17"/>
</mx:Application >