ThinkGeek - Cool Stuff for Geeks and Technophiles

December 12, 2003

AudioVideoStreamer with Flashcom. First Approach

yesterday i was playing with flashcom, and i decided to do an simple audiovideostreamer. Here are the codes. As you see is a really simple example, but it can show how easy is to work and have results with flash communication server

//net connection
nc=new NetConnection();

/*
Function: onStatusTemplate
Desc:	onStatus function to netConnection and netStream
params: 
info -> gets the info from the server
NOTE: 	this function, (onStatusTemplate) is based in Kevin Towes one.You can find this on his book "Macromedia Flash Communication Server MX" ( pangaea newmedia inc ) page 449, apendix. E
*/
function onStatusTemplate(info){
 	info_array=info.code.split(".");
 	trace("·······················Start the message");
 	trace(new Date());
 	trace("message received to object: "+info_array[0]);
 	trace("LEVEL: "+info.level);
 	trace("CODE: "+info.code);
 /*Here it comes the rest of the template. it's a really large code, so when i finish the entire app, i put it here to download
 */
}

//
NetConnection.prototype.onStatus=onStatusTemplate;
NetStream.prototype.onStatus=onStatusTemplate;
//Connect to server( below onStatus )
nc.connect("rtmp:/videolistado/videomusic");

/*
Function: audioVideoStreamer
Desc: 	Create objects of audiovideoStreamer class. It allows to play audios ( mp3 ) and videos ( flv ). It is defined as _global
Params:
streamName -> stream name
videoObjectName -> video object name
netConnectionName -> netconnection name
soundmcName -> mc to control volume
bufferTimeC ->  stablish buffer 
*/
_global.audioVideoStreamer=function(streamName, videoObjectName, netConnectionName, soundmcName, bufferTimeC){
 	//Asig.
 	this.my_ns=streamName;
 	this.my_video=videoObjectName;
 	this.my_nc=netConnectionName;
 	this.my_sound_mc=soundmcName;
 	this.my_buffer=bufferTimeC;
 	//
 	this.my_ns=new NetStream(this.my_nc);
 	this.my_video.attachVideo(this.my_ns);
 	this.my_ns.setBufferTime(this.my_buffer);
 	this.my_sound_mc.attachAudio(this.my_ns);
 	this.soundControl=new Sound(this.my_sound_mc);
 	this.soundControl.setVolume(50);
}//audioVideostreamer

/*
Function: doPlay
Desc: 	Starts to play the selected stream. note that is different the way we play a stream if it's an audio stream or a video stream
Params:
selectedStream -> selected stream
selectedType -> 2 possible values, "flv" or "mp3"
*/
audioVideoStreamer.prototype.doPlay=function(selectedStream,selectedType){
 	if(selectedType=="flv"){
  		this.my_ns.play(selectedStream);
  	}//flv
 	else if(selectedType=="mp3"){
  		this.my_ns.play("mp3:"+selectedStream);
  	}//mp3
};//doPlay

/*
Function: doRewing
Desc: 	rewing 10 seconds the stream
Params: no params
*/
audioVideoStreamer.prototype.doRewing=function(){
 	this.my_ns.seek(this.my_ns.time-10);
};//doRewing


/*
Function: doForward
Desc: 	forwar 10 seconds the stream 
Params: no params
*/
audioVideoStreamer.prototype.doForward=function(){
 	this.my_ns.seek(this.my_ns.time+10);
};//doForward


/*
Function: doPause
Desc: 	pause the stream
Params: no params
*/
audioVideoStreamer.prototype.doPause=function(){
 	this.my_ns.pause();
};//doPause


/*
Function: doDisconnect
Desc: 	Finish the stream
Params: no params
*/
audioVideoStreamer.prototype.doDisconnect=function(){
 	this.my_ns.play(false);
 	this.my_ns.clear();
};//doDisconnect

//////////////////////////USE of this
this.createEmptyMovieclip("mi_mc", 1);
reproductor=new audioVideoStreamer(el_stream, my_video, nc, mi_mc, 2);


video1={label:"Macromedia", data:"video macromedia", typeF:"flv"};
musica1={label:"Bob & Marcia", data:"young gifted and black", typeF:"mp3"};

mis_Streams=[video1, musica1];
my_lb.setDataProvider(mis_Streams);
my_lb.setChangeHandler("elegir", this);
elegir=function(c){
 	selectedStream=c.getSelectedItem().data;
 	selectedType=c.getSelectedItem().typeF;
 	_root.reproductor.doPlay(selectedStream, selectedType);
};



stop_btn.onRelease=function(){
 	reproductor.doDisconnect();
};
pause_btn.onRelease=function(){
 	reproductor.doPause();
};
rewing_btn.onRelease=function(){
 	reproductor.doRewing();								
};
forward_btn.onRelease=function(){
 	reproductor.doForward();
};

of course i have on the screen an video object: my_video, a listbox component: my_lb and buttons.: stop_btn, pause_btn, rewing_btn, forward_btn

It will continue...

As always, sorry for my terrible english :(

Posted by Javier Tardáguila Date: December 12, 2003 10:22 AM
Comments