I've always wondered how to code my flash files to detect a user bandwidth. Well I've found it. The tools you need are Flash Communication Server 2 and Flash 8 and some experience with coding. The process is quite simple and logical considering if you are delivering content to the public and have to compensate for users with different connection speeds; bandwidths.
Here's the code -
1 nc = new NetConnection();
2 nc.onStatus = function(info) {
3 trace("Level: "+info.level+" Code: "+info.code);
4 if (info.code == "NetConnection.Connect.Success") {
5 trace("--- connected to: " + this.uri);
6 initStream();
7 }
8 };
9 nc.connect("rtmp:/videoplayer");
10
11 initStream = function(){
12 // create a NetStream
13 video_ns = new NetStream(nc);
14 // optional onStatus handler
15 video_ns.onStatus = function(info) {
16 trace("Level: " + info.level + " Code: " + info.code);
17 };
18 // attach the NetStream to a video object called videoObj on stage
19 videoObj.attachVideo(video_ns);
20 video_ns.play("100kbps_Stream", 0); // the parameter 0 will tell the server to play a recorded stream
21 }
22 stop();
Add the following to the frame in Flash 8 -
1 nc = new NetConnection();
2 nc.onStatus = function(info) {
3 trace("Level: " + info.level + " Code: " + info.code);
4 if (info.code == "NetConnection.Connect.Success") {
5 trace("--- connected to: " + this.uri);
7 }
8 };
9 NetConnection.prototype.onBWDone = function(p_bw) {
10 trace("onBWDone: "+p_bw);
11 }
12 NetConnection.prototype.onBWCheck = function() {
13 return ++counter; // Serverside, just ignore any return value and return the call count
14 }
15 nc.connect("rtmp:/bwcheck", true);
16 stop();
Now, in your FLA file add the following -
detected_bw = p_bw;
// close the NetConnection to bwcheck
this.close();
gotoAndStop(2);
To request the appropriate stream -
if(detected_bw >= 256){
useVideo = "256kbps_Stream";
bufferlength = 2;
} else if (detected_bw <256 && detected_bw >=100){ useVideo = "100kbps_Stream"; bufferlength = 3; } else { // bandwidth is too low, you best alert the user at this stage trace("bandwidth too low"); }
trace("bandwidth too low");
}
For the onStatus handler -
video_ns.onStatus = function(info) {
trace("Level: " + info.level + " Code: " + info.code);
};
dd this code to set the bufferlength:
video_ns.setBufferTime(bufferlength);
Also modify the play action by changing that line from this:
video_ns.play("100kbps_Stream", 0);
to this:
video_ns.play(useVideo, 0);
I will like to thank Stefan Richter and Adobe for the help.
Thursday, September 28, 2006
Subscribe to:
Posts (Atom)