Creating one playlist with Flash Communication Server/Flash Media Server.

We saw in the previous article as to record they stream using the Flash Communication Server/Flash Media Server, now we go to implement one playlist for this resource. Each video that to record will be added playlist for later being seen.

For this we will use the SharedObject Remote to keep the information of the recorded videos. SharedObjects offers sharing of information in real time between multiplos customers SWF’ s being able to be persistent in schemes local or in the remote server. The Shared place object would be as “Cookies” and the SharedObject Remote as transference of data in real-time.

It opens a new document in the Flash, we go to create two movies clips of the video type, to add to a video object in its library, opens the panel library (windom Video object you your library, open the Library panel (Window > Library or Ctrl+L or F11) adds to one embedded Video object to it selecting New Video in the options of library.

First connecting to the Flashcom/Flash Media Server:

[as]
client_nc = new NetConnection();
client_nc.onStatus = function(info) {
trace(“Level: “+info.level+” Code: “+info.code);
};
client_nc.connect(“rtmp:/playlist”);//considerando localhost
[/as]

Recouping the data of the Shared Object Remote, and filling component “List” using the method onSync, this method is the responsible one for the synchronization of data with the server.

[as]
rec_so = SharedObject.getRemote(“videos”, client_nc.uri, true);
rec_so.onSync = function(list) {
_root.Play_list.removeAll();
for (var i in _root.rec_so.data) {
_root.Play_list.addItem(i);
}
};
rec_so.connect(client_nc);
[/as]

In the first line it notices that we give the name of “video” to the object created, the third parameter “true” will guarantee that the data remain in the server, in case that parameter were passed falsifies, the data so would remain while the user was connected to the application. In stage we will have two textfields, one with the name of instance of “ListItem” of the type “input” and another one with the o name of instance of “Status_msg” of the type “dynamic”, also will have two movie clips of the type video, one to receive it webcam with the name of instance of “live” and other to show the selected videos of the list with “the published” name of instance of. We create a function to record stream it:

[as]function doRecord() {
if (ListItem.text == undefined || ListItem.text == “”) {
Status_msg.text = “Necessario um titulo.”;
ListItem.setFocus();
} else if (Record_btn.label == “Record”) {
Status_msg.text = “Recording…”;
if (Play_btn.label == “Stop”) {
doPlay();
}
Play_btn.enabled = false;
out_ns = new NetStream(_root.client_nc);
out_ns.attachAudio(Microphone.get());
var teste_cam:Camera = Camera.get();
live.attachVideo(teste_cam);
out_ns.attachVideo(teste_cam);
out_ns.publish(ListItem.text, “record”);
_root.rec_so.data[ListItem.text] = ListItem.text;
_root.rec_so.flush();
Record_btn.label = “Stop”;
} else if (Record_btn.label == “Stop”) {
out_ns.close();
Play_btn.enabled = true;
Record_btn.label = “Record”;
ListItem.text = “”;
Status_msg.text = “…”;
}
}
[/as]

Interesting detail in the method “publish” of the NetStream class, that can have three arguments, its syntax is the following one:

  • record: saved the archive as flv in the server, if the archive already to exist will be sobrescrito.
  • append: saved the archive as flv in the server, adding from the safe archive previously.
  • live: it transmits to the living without saving, if the archive to exist will be deleted.

Creating the method to play the video of the list.

[as]
function doPlay() {
if (Play_btn.label == “Play”) {
Status_msg.text = “Playing…”;
Play_btn.label = “Stop”;
var playFileName = Play_list.selectedItem.label;
_root.in_ns = new NetStream(_root.client_nc);
publicado.attachVideo(_root.in_ns);
_root.in_ns.play(playFileName);
in_ns.onStatus = function(info) {
if (info.level == “error” || info.code == “NetStream.Play.Stop”) {
Status_msg.text = “Stopped sending data…”;
Play_btn.label = “Play”;
}
};
} else if (Play_btn.label == “Stop”) {
Status_msg.text = “Stop…”;
in_ns.onStatus = null;
in_ns.close();
Play_btn.label = “Play”;
}
}
[/as]

Finishing with the creation dynamically of the buttons, the list and associating the methods doRecord and doPlay to the events of the buttons.

[as]
Play_list.addEventListener(“change”, listListener);
var Play_list = _root.createClassObject(mx.controls.List, “Play_list”, 1, {_x:357, _y:214});
Play_list.setSize(148, 177);
var Play_btn = _root.createClassObject(mx.controls.Button, “Play_btn”, 2, {_x:170, _y:216, label:”Play”});
var Record_btn = _root.createClassObject(mx.controls.Button, “Record_btn”, 3, {_x:47, _y:216, label:”Record”});
Record_btn.onRelease = function() {
doRecord();
};
Play_btn.onRelease = function() {
doPlay();
};
var listListener:Object = new Object();
listListener.change = function(evt_obj:Object) {
var playFileName = evt_obj.target.selectedItem.label;
trace(playFileName);
in_ns.pause();
in_ns = new NetStream(_root.client_nc);
publicado.attachVideo(in_ns);
in_ns.play(playFileName);
};
[/as]

The code complete:
[as]
client_nc = new NetConnection();
client_nc.onStatus = function(info) {
trace(“Level: “+info.level+” Code: “+info.code);
};
client_nc.connect(“rtmp://200.219.239.68/osfederais/playlist”);
rec_so = SharedObject.getRemote(“recordings”, client_nc.uri, true);
rec_so.onSync = function(list) {
_root.Play_list.removeAll();
for (var i in _root.rec_so.data) {
_root.Play_list.addItem(i);
}
};
rec_so.connect(client_nc);
function doRecord() {
if (ListItem.text == undefined || ListItem.text == “”) {
Status_msg.text = “Necessario um titulo.”;
ListItem.setFocus();
} else if (Record_btn.label == “Record”) {
Status_msg.text = “Recording…”;
if (Play_btn.label == “Stop”) {
doPlay();
}
Play_btn.enabled = false;
_root.out_ns = new NetStream(_root.client_nc);
_root.out_ns.attachAudio(Microphone.get());
_root.teste_cam = Camera.get();
_root.live.attachVideo(teste_cam);
_root.out_ns.attachVideo(_root.teste_cam);
_root.out_ns.publish(ListItem.text, “record”);
_root.rec_so.data[ListItem.text] = ListItem.text;
_root.rec_so.flush();
Record_btn.label = “Stop”;
} else if (Record_btn.label == “Stop”) {
_root.out_ns.close();
Play_btn.enabled = true;
Record_btn.label = “Record”;
ListItem.text = “”;
Status_msg.text = “…”;
}
}
function doPlay() {
if (Play_btn.label == “Play”) {
Status_msg.text = “Playing…”;
Play_btn.label = “Stop”;
var playFileName = Play_list.selectedItem.label;
_root.in_ns = new NetStream(_root.client_nc);
publicado.attachVideo(_root.in_ns);
_root.in_ns.play(playFileName);
_root.in_ns.onStatus = function(info) {
if (info.level == “error” || info.code == “NetStream.Play.Stop”) {
Status_msg.text = “parado…”;
Play_btn.label = “Play”;
}
};
} else if (Play_btn.label == “Stop”) {
Status_msg.text = “Stop…”;
_root.in_ns.onStatus = null;
_root.in_ns.close();
Play_btn.label = “Play”;
}
}
Play_list.addEventListener(“change”, cbListener);
var Play_list = _root.createClassObject(mx.controls.List, “Play_list”, 1, {_x:357, _y:214});
Play_list.setSize(148, 177);
var Play_btn = _root.createClassObject(mx.controls.Button, “Play_btn”, 2, {_x:170, _y:216, label:”Play”});
var Record_btn = _root.createClassObject(mx.controls.Button, “Record_btn”, 3, {_x:47, _y:216, label:”Record”});
Record_btn.onRelease = function() {
doRecord();
};
Play_btn.onRelease = function() {
doPlay();
};
var cbListener:Object = new Object();
cbListener.change = function(evt_obj:Object) {
var playFileName = evt_obj.target.selectedItem.label;
trace(playFileName);
in_ns.pause();
in_ns = new NetStream(_root.client_nc);
publicado.attachVideo(in_ns);
in_ns.play(playFileName);
};
[/as]

To define its application in the server, it creates a diretory called “playlist” in its server the Flash Communication Server/Flash Media Server. It gives to ctrl+enter to test the application
It sees application in functioning

more
Flash Media Server Developer Center
http://www.macromedia.com/devnet/flashmediaserver/

FlashComGuru
http://www.flashcomguru.com/

FlashCom.com.br
http://www.flashcom.com.br/

Flash Media Server Fun
http://www.fczone.com/

2 thoughts on “Creating one playlist with Flash Communication Server/Flash Media Server.

Leave a Reply