ActionScript 3 AS3 NetConnectShare类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3 NetConnectShare类相关的知识,希望对你有一定的参考价值。
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.events.TimerEvent;
import flash.media.SoundTransform;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.utils.Timer;
public class NetConnectShare extends Sprite
{
private var videoURL:String = null;
public static var connection:NetConnection = new NetConnection();
public var stream:NetStream;
private static var tim:Timer;
private var muted:Boolean = false;
private var previousPlayheadTime:Number;
private static var pause:Boolean = false;
private var currentVolume:Number = 1;
private var cuePoints:Boolean = false;
private var ASCuePoints:Array = new Array();
private var intBad:int = 0;
public var hasStream:Boolean = false;
public var video:Video;
public function NetConnectShare( _video:Video )
{
// set the output
video = _video;
// create listeners for connection
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
}
public function connect( _videoURL:String, _video:Video = null ):void
{
// set the source url
videoURL = _videoURL;
if (_video != null)
{
video = _video;
}
// create connection
connection.connect(null);
}
public function switchPlayers( _video:Video ):void
{
trace("[switchPlayers]", "net" );
// assign output and attatch stream
video = _video;
video.attachNetStream(stream);
}
public function playheadTime():Number
{
trace("[playheadTime]", "playheadTime" );
if (stream)
{
return stream.time;
}
else
{
return 0;
}
}
public function play( url:String ):void
{
trace( "[play] pause = false", "net" );
// assign source and play stream
videoURL = url;
playStream();
pause = false;
}
public function stop():void
{
trace( "[stop] pause = true", "net" );
// stop timer and stream
if (tim)
{
tim.stop();
}
stream.pause();
pause = true;
}
public function pausePlay( e:Event = null ):void
{
pause = ! pause;
trace( "[pausePlay] pause = " + String( pause ) + " isEvent = " + String( Boolean( e ) ) , "net" );
//toggle pause / play and if pausing stop timer, if playing start or create timer
if (pause)
{
trace( "[pausePlay] STOP " + String( tim ) + " pause = " + String( pause ) , "net" );
if (tim)
{
tim.stop();
}
}
else
{
if (tim)
{
trace( "[pausePlay] START " + String( tim ) + " pause = " + String( pause ) , "net" );
tim.start();
}
else
{
trace( "[pausePlay] NEW TIM pause = " + String( pause ) , "net" );
try
{
tim.stop();
trace("[pausePlay] old tim ");
}
catch (e:Error)
{
trace("[pausePlay] old tim error");
}
tim = new Timer(500);
tim.addEventListener(TimerEvent.TIMER, timeCheck );
tim.start();
}
}
try
{
stream.togglePause();
}
catch (e:Error)
{
trace( "[pausePlay] ERROR \n" + e );
}
}
public function playOn( e:Event = null ):void
{
trace( "[playOn] pause = " + String( pause ) + " isEvent = " + String( Boolean( e ) ) , "net" );
// make sure stream is pause and togglePause to play
if (! pause)
{
if (tim)
{
trace( "[playOn] START " + String( tim ) + " pause = " + String( pause ) , "net" );
tim.start();
stream.pause();
try
{
stream.togglePause();
}
catch (e:Error)
{
trace( "[pausePlay] ERROR \n" + e );
}
}
else
{
trace( "[playOn] NEW TIM pause = " + String( pause ) , "net" );
stream.pause();
try
{
stream.togglePause();
}
catch (e:Error)
{
trace( "[pausePlay] ERROR \n" + e );
}
tim = new Timer(500);
tim.addEventListener(TimerEvent.TIMER, timeCheck );
tim.start();
}
}
}
public function seek( num:Number ):void
{
trace( "[seek]", "net" );
stream.seek( num );
}
public function set volume( num:Number ):void
{
trace("[volume]", "net" );
var sT:SoundTransform = new SoundTransform(num);
stream.soundTransform = sT;
currentVolume = num;
}
public function get volume():Number
{
return currentVolume;
}
public function mute( e:Event = null ):void
{
var sT:SoundTransform;
// toggle mute
if (muted)
{
sT = new SoundTransform(1,0);
stream.soundTransform = sT;
currentVolume = 1;
}
else
{
sT = new SoundTransform(0,0);
stream.soundTransform = sT;
currentVolume = 0;
}
muted = ! muted;
trace("[mute] "+ String( muted ) , "net" );
}
public function isMuted():Boolean
{
return muted;
}
public function get source():String
{
return this.videoURL;
}
public function set source( _videoURL:String ):void
{
videoURL = _videoURL;
}
public function addASCuePoint( _time:Number, name:String ):void
{
var tArr:Array = new Array();
// add cue point as multidymentional array
// [ timeOfCuePoin, nameOfCuePoint, hasCuePointBeenPassed ]
tArr = [_time,name,false];
cuePoints = true;
ASCuePoints.push( tArr );
}
public function get bytesLoaded():Number
{
return stream.bytesLoaded;
}
public function get bytesTotal():Number
{
return stream.bytesTotal;
}
public function get totalTime():Number
{
return stream.client.duration;
}
public function reConnectStream():void
{
trace( "[reConnectStream]", "net" );
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.client = new CustomClient();
video.attachNetStream(stream);
playStream();
}
// Private Methods
private function netStatusHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetConnection.Connect.Success" :
connectStream();
break;
case "NetStream.Play.StreamNotFound" :
trace("Stream not found: " + videoURL + "\n" , "net" );
break;
}
}
private function securityErrorHandler(event:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + event);
}
private function connectStream():void
{
trace( "[connectStream]", "net" );
if (! this.hasStream && videoURL)
{
trace("SNAP "+ String( hasStream ) +" "+ String( videoURL ), "net" );
this.hasStream = true;
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.client = new CustomClient();
video.attachNetStream(stream);
playStream();
}
}
private function playStream():void
{
trace("[playStream] pause = false", "net" );
intBad = 0;
stream.client.metaRec = false;
stream.play(videoURL);
pause = false;
tim = new Timer(500);
tim.addEventListener(TimerEvent.TIMER, timeCheck );
tim.start();
}
private function timeCheck( e:TimerEvent ):void
{
trace("[timeCheck] pause = " + String( pause ), "net");
var i:uint;
var myPlayheadTime:Number = this.playheadTime();
// end video if at or beyond the meta data duration
if (stream.client.metaRec)
{
if ( ( myPlayheadTime >= stream.client.duration ) )
{
trace("MetaRec: " + "\n", "net" );
stream.client.onPlayStatus();
stream.close();
this.dispatchEvent( new Event ( "videoPlayComplete", true ) );
}
}
// end video even if onPlayStatus does not fire
if (myPlayheadTime == this.previousPlayheadTime)
{
if (! pause)
{
++intBad;
if (intBad > 8)
{
trace("intBad: pause = " + String( pause ) + "\n", "net" );
stream.client.onPlayStatus();
stream.close();
this.dispatchEvent( new Event ( "videoPlayComplete", true ) );
intBad = 0;
}
}
}
else
{
intBad = 0;
}
this.previousPlayheadTime = myPlayheadTime;
// check if passed cue point and if so fire event
if (cuePoints)
{
for (i = 0; i < ASCuePoints.length; i++)
{
if (! ASCuePoints[i][2])
{
if (this.playheadTime() > ASCuePoints[i][0])
{
this.dispatchEvent( new Event ( ASCuePoints[ i ][ 1 ] + "CuePoint", true) );
ASCuePoints[i][2] = true;
}
}
}
}
}
public function get paused():Boolean
{
return pause;
}
}
}
/*
import common.utils.Loggit;
public class CustomClient
{
public var duration:Number;
public var width:Number;
public var height:Number;
public var metaRec:Boolean;
public var cuePoints:Array = new Array();
public function onPlayStatus(info:Object = null ):void
{
trace("onPlayStatus: code=complete" + "\n", "net" );
}
public function onMetaData(info:Object):void
{
trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate + "\n", "net" );
// assign returned meta data
duration = info.duration;
width = info.width;
height = info.height;
this.metaRec = true;
}
public function onCuePoint(info:Object):void
{
trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type + "\n", "net" );
cuePoints.push( [ info.time, info.name, info.type ] );
}
}
*/
以上是关于ActionScript 3 AS3 NetConnectShare类的主要内容,如果未能解决你的问题,请参考以下文章
[ActionScript 3.0] AS3.0 水面波纹效果