Highlighted

netstream about play() And seek()?

Community Beginner ,
Apr 03, 2019

Copy link to clipboard

Copied

when the first video is playing ,I play second video,The first video will show Carton. (I used play(url ) function);

or

I play two video   at the same time. after five sec, seek first video ,and second video will show Carton.(I used seek(0));

code:

package ActionScript.Model.video_module{

import flash.events.*;

import flash.media.Video;

import flash.display.Sprite;

import flash.net.*;

import flash.text.TextField;

import fl.controls.Button;

import flash.events.EventDispatcher;

import com.greensock.TweenLite;

import ActionScript.Model.debug_module.Debug;

import ActionScript.Model.CustomEvent;

import flash.utils.Timer;

import flash.media.SoundTransform;

public class StreamingVideoPlayer extends Sprite {

public var _videoURL: String;

private var _video: Video;

private var _vidDuration: Number;

private var _vidXmax: Number;

private var _vidYmax: Number;

private var _vidWidth: Number;

private var _vidHeight: Number;

private var _main_nc: NetConnection = new NetConnection();

private var _serverLoc: String;

private var _ns: NetStream;

private var _isReady:Boolean=false;

private var _totalTime:Number=0;

private var _isPlay:Boolean=false;

private var _bg:Sprite=new Sprite();

public var  loop:Boolean=false;

public var isSeekCompelte:Boolean=true;

private var trans:SoundTransform=null;

private var Id:int=0;

private var defaultSize:Boolean=false;

private var infoArr:Array=new Array();

/* ------------Contructors/Initialization-----------*/

public function StreamingVideoPlayer(flvLocation: String="",vidXmax: Number=1920,vidYmax: Number=1080,vidDuration: Number=0,serverLoc: String=null): void {

//set video params

_isReady=false;

_videoURL = flvLocation;

_vidDuration = vidDuration;

_vidXmax = vidXmax;

_vidYmax = vidYmax;

//add eventListeners to NetConnection and connect

_main_nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);

_main_nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);

_main_nc.connect(serverLoc);

}

private function createBt():void{

_bg.graphics.clear();

_bg.graphics.beginFill(0x000000,0);

_bg.graphics.drawRect(0,0,_vidXmax,_vidYmax);

_bg.graphics.endFill();

}

private function onNetStatus(event: Object): void {

trace(event.info.code);

switch (event.info.code) {

case "NetConnection.Connect.Success":

//play stream if connection successful

connectStream();

//log("NetConnection.Connect.Success");

break;

case "NetStream.Play.StreamNotFound":

break;

case "NetStream.Play.Stop":

pauseFlv();

videoPlayComplete();

break;

case "NetStream.Buffer.Empty":

pauseFlv();

seekFlv();

  break;

case "NetStream.Buffer.Full":

if(_ns.time!=0)return;

_bg.visible=false;

        onVideoReady();

break;

case "NetStream.Buffer.Flush":

break;

case"NetStream.Seek.Complete":

if(_ns.time!=0){

seekFlv();

return;

}

break;

}

}

/* -------------------Connection------------------- */

private function connectStream(): void {

//netstream object

_ns = new NetStream(_main_nc);

_ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler);

_ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);

//other event handlers assigned 

//to the netstream client property

var custom_obj: Object = new Object();

custom_obj.onMetaData = onMetaDataHandler;

custom_obj.onCuePoint = onCuePointHandler;

//custom_obj.onPlayStatus = playStatus;

_ns.client = custom_obj;

_ns.bufferTime=0;

//attach netstream to the video object

_video = new Video(_vidXmax, _vidYmax);

_video.attachNetStream(_ns);

//video position could be parameterized but hardcoded

//here to account for optional movie frame border

_video.addEventListener(VideoEvent.RENDER_STATE, videoStateChange);

//_ns.useHardwareDecoder=true;

//_ns.inBufferSeek=true;

addChild(_video);

createBt();

addChild(_bg);

}

private function videoStateChange(e:VideoEvent):void{

/*txt.text=String(_ns.useHardwareDecoder)+"\n";

txt.text+=e.status;*/

}

/* -------------NetStream actions and events-------------- */

private function videoPlayComplete(): void {

onPlayFinish();

}

public function restart():void{

seekFlv();

}

public function ondispose():void{

_video.clear();

_ns.dispose();

}

public function destroy():void{

ondispose();

_bg.graphics.clear();

if(this.contains(_bg)){

this.removeChild(_bg);

}

}

//视频准备就绪

private function onVideoReady():void{

//log(Id+"准备:"+String(_ns.time));

//log("视频已经准备就绪!");

var evt:CustomEvent=new CustomEvent(CustomEvent.LOADSUCCESS);

evt.Name="LOADSUCCESS";

this.dispatchEvent(evt);

}

//视频播放完成

private function onPlayFinish():void{

//log("视频播放结束!")

var evt:CustomEvent=new CustomEvent(CustomEvent.VIDEO_FINISH);

evt.Name="VIDEO_FINISH";

this.dispatchEvent(evt);

}

//加载视频

public function onLoadFlv():void{

_ns.play(_videoURL);

//seekFlv(0);

pauseFlv();

/*_bg.visible=true;

trans= _ns.soundTransform;*/

if(_ns.time!=0){

log(String(_ns.time));

}

}

//音量设置

public function set volume(vaule:Number):void{ 

if(_ns){

trans.volume =vaule;

_ns.soundTransform =trans;

}

        }

//获取音量

public function get volume():Number{ 

trans= _ns.soundTransform;

var vaule=trans.volume;

return vaule

        }

//跳转

public function seekFlv(_offset:Number=0):void{

if(_offset==0){

_ns.seek(0);

_bg.visible=false;

}else{

var postion:Number=_ns.time+_offset;

if(postion<=_totalTime){

_ns.seek(postion);

}else{

_ns.seek(_totalTime);

}

}

}

//当期视频播放的时间

public function getPostion():Number{

return _ns.time;

}

//当期视频的总时长

public function getTotalTime():Number{

return _totalTime;

}

//播放视频

public function playFlv(): void {

//_bg.visible=false;

if(_ns.time!=0){

log("ID:"+Id+"\t:"+getPostion());

log(String(infoArr));

log("--------------------------------------------")

}

_ns.resume();

infoArr=[]

}

//暂停视频

public function pauseFlv(): void {

_ns.pause();

}

public function stopFlv():void{

pauseFlv();

seekFlv(0);

_bg.visible=true;

}

public function set id(_id:int):void{

Id=_id;

}

public function get id():int{

return Id;

}

/* -----------------Information handlers---------------- */

private function onMetaDataHandler(metaInfoObj: Object): void {

/************设置视频的尺寸***************/

if(_vidXmax!=metaInfoObj.width){

_video.width = metaInfoObj.width;

_vidWidth = metaInfoObj.width;

_video.height = metaInfoObj.height;

_vidHeight = metaInfoObj.height;

}

_totalTime=metaInfoObj.duration;

_ns.bufferTime=0.1;

//_ns.bufferTimeMax=40;

}

private function onCuePointHandler(cueInfoObj: Object): void {

/*trace("cuepoint: time=" + cueInfoObj.time + " name=" + 

cueInfoObj.name + " type=" + cueInfoObj.type); */

}

/* -----------------------Error handlers------------------------ */

private function securityErrorHandler(event: SecurityErrorEvent): void {

trace("securityErrorHandler: " + event);

}

private function asyncErrorHandler(event: AsyncErrorEvent): void {

trace(event.text);

}

public function log(message:String):void {

Debug.getInstance().txtTip(message);

        }

}

}

TOPICS
Development

Views

337

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

netstream about play() And seek()?

Community Beginner ,
Apr 03, 2019

Copy link to clipboard

Copied

when the first video is playing ,I play second video,The first video will show Carton. (I used play(url ) function);

or

I play two video   at the same time. after five sec, seek first video ,and second video will show Carton.(I used seek(0));

code:

package ActionScript.Model.video_module{

import flash.events.*;

import flash.media.Video;

import flash.display.Sprite;

import flash.net.*;

import flash.text.TextField;

import fl.controls.Button;

import flash.events.EventDispatcher;

import com.greensock.TweenLite;

import ActionScript.Model.debug_module.Debug;

import ActionScript.Model.CustomEvent;

import flash.utils.Timer;

import flash.media.SoundTransform;

public class StreamingVideoPlayer extends Sprite {

public var _videoURL: String;

private var _video: Video;

private var _vidDuration: Number;

private var _vidXmax: Number;

private var _vidYmax: Number;

private var _vidWidth: Number;

private var _vidHeight: Number;

private var _main_nc: NetConnection = new NetConnection();

private var _serverLoc: String;

private var _ns: NetStream;

private var _isReady:Boolean=false;

private var _totalTime:Number=0;

private var _isPlay:Boolean=false;

private var _bg:Sprite=new Sprite();

public var  loop:Boolean=false;

public var isSeekCompelte:Boolean=true;

private var trans:SoundTransform=null;

private var Id:int=0;

private var defaultSize:Boolean=false;

private var infoArr:Array=new Array();

/* ------------Contructors/Initialization-----------*/

public function StreamingVideoPlayer(flvLocation: String="",vidXmax: Number=1920,vidYmax: Number=1080,vidDuration: Number=0,serverLoc: String=null): void {

//set video params

_isReady=false;

_videoURL = flvLocation;

_vidDuration = vidDuration;

_vidXmax = vidXmax;

_vidYmax = vidYmax;

//add eventListeners to NetConnection and connect

_main_nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);

_main_nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);

_main_nc.connect(serverLoc);

}

private function createBt():void{

_bg.graphics.clear();

_bg.graphics.beginFill(0x000000,0);

_bg.graphics.drawRect(0,0,_vidXmax,_vidYmax);

_bg.graphics.endFill();

}

private function onNetStatus(event: Object): void {

trace(event.info.code);

switch (event.info.code) {

case "NetConnection.Connect.Success":

//play stream if connection successful

connectStream();

//log("NetConnection.Connect.Success");

break;

case "NetStream.Play.StreamNotFound":

break;

case "NetStream.Play.Stop":

pauseFlv();

videoPlayComplete();

break;

case "NetStream.Buffer.Empty":

pauseFlv();

seekFlv();

  break;

case "NetStream.Buffer.Full":

if(_ns.time!=0)return;

_bg.visible=false;

        onVideoReady();

break;

case "NetStream.Buffer.Flush":

break;

case"NetStream.Seek.Complete":

if(_ns.time!=0){

seekFlv();

return;

}

break;

}

}

/* -------------------Connection------------------- */

private function connectStream(): void {

//netstream object

_ns = new NetStream(_main_nc);

_ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler);

_ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);

//other event handlers assigned 

//to the netstream client property

var custom_obj: Object = new Object();

custom_obj.onMetaData = onMetaDataHandler;

custom_obj.onCuePoint = onCuePointHandler;

//custom_obj.onPlayStatus = playStatus;

_ns.client = custom_obj;

_ns.bufferTime=0;

//attach netstream to the video object

_video = new Video(_vidXmax, _vidYmax);

_video.attachNetStream(_ns);

//video position could be parameterized but hardcoded

//here to account for optional movie frame border

_video.addEventListener(VideoEvent.RENDER_STATE, videoStateChange);

//_ns.useHardwareDecoder=true;

//_ns.inBufferSeek=true;

addChild(_video);

createBt();

addChild(_bg);

}

private function videoStateChange(e:VideoEvent):void{

/*txt.text=String(_ns.useHardwareDecoder)+"\n";

txt.text+=e.status;*/

}

/* -------------NetStream actions and events-------------- */

private function videoPlayComplete(): void {

onPlayFinish();

}

public function restart():void{

seekFlv();

}

public function ondispose():void{

_video.clear();

_ns.dispose();

}

public function destroy():void{

ondispose();

_bg.graphics.clear();

if(this.contains(_bg)){

this.removeChild(_bg);

}

}

//视频准备就绪

private function onVideoReady():void{

//log(Id+"准备:"+String(_ns.time));

//log("视频已经准备就绪!");

var evt:CustomEvent=new CustomEvent(CustomEvent.LOADSUCCESS);

evt.Name="LOADSUCCESS";

this.dispatchEvent(evt);

}

//视频播放完成

private function onPlayFinish():void{

//log("视频播放结束!")

var evt:CustomEvent=new CustomEvent(CustomEvent.VIDEO_FINISH);

evt.Name="VIDEO_FINISH";

this.dispatchEvent(evt);

}

//加载视频

public function onLoadFlv():void{

_ns.play(_videoURL);

//seekFlv(0);

pauseFlv();

/*_bg.visible=true;

trans= _ns.soundTransform;*/

if(_ns.time!=0){

log(String(_ns.time));

}

}

//音量设置

public function set volume(vaule:Number):void{ 

if(_ns){

trans.volume =vaule;

_ns.soundTransform =trans;

}

        }

//获取音量

public function get volume():Number{ 

trans= _ns.soundTransform;

var vaule=trans.volume;

return vaule

        }

//跳转

public function seekFlv(_offset:Number=0):void{

if(_offset==0){

_ns.seek(0);

_bg.visible=false;

}else{

var postion:Number=_ns.time+_offset;

if(postion<=_totalTime){

_ns.seek(postion);

}else{

_ns.seek(_totalTime);

}

}

}

//当期视频播放的时间

public function getPostion():Number{

return _ns.time;

}

//当期视频的总时长

public function getTotalTime():Number{

return _totalTime;

}

//播放视频

public function playFlv(): void {

//_bg.visible=false;

if(_ns.time!=0){

log("ID:"+Id+"\t:"+getPostion());

log(String(infoArr));

log("--------------------------------------------")

}

_ns.resume();

infoArr=[]

}

//暂停视频

public function pauseFlv(): void {

_ns.pause();

}

public function stopFlv():void{

pauseFlv();

seekFlv(0);

_bg.visible=true;

}

public function set id(_id:int):void{

Id=_id;

}

public function get id():int{

return Id;

}

/* -----------------Information handlers---------------- */

private function onMetaDataHandler(metaInfoObj: Object): void {

/************设置视频的尺寸***************/

if(_vidXmax!=metaInfoObj.width){

_video.width = metaInfoObj.width;

_vidWidth = metaInfoObj.width;

_video.height = metaInfoObj.height;

_vidHeight = metaInfoObj.height;

}

_totalTime=metaInfoObj.duration;

_ns.bufferTime=0.1;

//_ns.bufferTimeMax=40;

}

private function onCuePointHandler(cueInfoObj: Object): void {

/*trace("cuepoint: time=" + cueInfoObj.time + " name=" + 

cueInfoObj.name + " type=" + cueInfoObj.type); */

}

/* -----------------------Error handlers------------------------ */

private function securityErrorHandler(event: SecurityErrorEvent): void {

trace("securityErrorHandler: " + event);

}

private function asyncErrorHandler(event: AsyncErrorEvent): void {

trace(event.text);

}

public function log(message:String):void {

Debug.getInstance().txtTip(message);

        }

}

}

TOPICS
Development

Views

338

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 03, 2019 0

Have something to add?

Join the conversation