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:

ActionScript

  1. client_nc = new NetConnection();
  2. client_nc.onStatus = function(info) {
  3.        trace("Level: "+info.level+"   Code: "+info.code);
  4. };
  5. client_nc.connect("rtmp:/playlist");//considerando localhost

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.

ActionScript

  1. rec_so = SharedObject.getRemote("videos", client_nc.uri, true);
  2. rec_so.onSync = function(list) {
  3.         _root.Play_list.removeAll();
  4.        for (var i in _root.rec_so.data) {
  5.                     _root.Play_list.addItem(i);
  6.       }
  7. };
  8. rec_so.connect(client_nc);

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:

ActionScript

  1. function doRecord() {
  2.     if (ListItem.text == undefined || ListItem.text == "") {
  3.         Status_msg.text = "Necessario um titulo.";
  4.         ListItem.setFocus();
  5.     } else if (Record_btn.label == "Record") {
  6.         Status_msg.text = "Recording...";
  7.         if (Play_btn.label == "Stop") {
  8.             doPlay();
  9.         }
  10.         Play_btn.enabled = false;
  11.         out_ns = new NetStream(_root.client_nc);
  12.         out_ns.attachAudio(Microphone.get());
  13.         var teste_cam:Camera = Camera.get();
  14.         live.attachVideo(teste_cam);
  15.         out_ns.attachVideo(teste_cam);
  16.         out_ns.publish(ListItem.text, "record");
  17.         _root.rec_so.data[ListItem.text] = ListItem.text;
  18.         _root.rec_so.flush();
  19.         Record_btn.label = "Stop";
  20.     } else if (Record_btn.label == "Stop") {
  21.         out_ns.close();
  22.         Play_btn.enabled = true;
  23.         Record_btn.label = "Record";
  24.         ListItem.text = "";
  25.         Status_msg.text = "...";
  26.     }
  27. }

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.

ActionScript

  1. function doPlay() {
  2.     if (Play_btn.label == "Play") {
  3.         Status_msg.text = "Playing...";
  4.         Play_btn.label = "Stop";
  5.         var playFileName = Play_list.selectedItem.label;
  6.         _root.in_ns = new NetStream(_root.client_nc);
  7.         publicado.attachVideo(_root.in_ns);
  8.         _root.in_ns.play(playFileName);
  9.         in_ns.onStatus = function(info) {
  10.         if (info.level == "error" || info.code == "NetStream.Play.Stop") {
  11.             Status_msg.text = "Stopped sending data...";
  12.                     Play_btn.label = "Play";
  13.         }
  14.         };
  15.     } else if (Play_btn.label == "Stop") {
  16.         Status_msg.text = "Stop...";
  17.         in_ns.onStatus = null;
  18.         in_ns.close();
  19.         Play_btn.label = "Play";
  20.        }
  21. }

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

ActionScript

  1. Play_list.addEventListener("change", listListener);
  2. var Play_list = _root.createClassObject(mx.controls.List, "Play_list", 1, {_x:357, _y:214});
  3. Play_list.setSize(148, 177);
  4. var Play_btn = _root.createClassObject(mx.controls.Button, "Play_btn", 2, {_x:170, _y:216, label:"Play"});
  5. var Record_btn = _root.createClassObject(mx.controls.Button, "Record_btn", 3, {_x:47, _y:216, label:"Record"});
  6. Record_btn.onRelease = function() {
  7.        doRecord();
  8. };
  9. Play_btn.onRelease = function() {
  10.        doPlay();
  11. };
  12. var listListener:Object = new Object();
  13. listListener.change = function(evt_obj:Object) {
  14.     var playFileName = evt_obj.target.selectedItem.label;
  15.     trace(playFileName);
  16.     in_ns.pause();
  17.     in_ns = new NetStream(_root.client_nc);
  18.     publicado.attachVideo(in_ns);
  19.     in_ns.play(playFileName);
  20. };

The code complete:

ActionScript

  1. client_nc = new NetConnection();
  2. client_nc.onStatus = function(info) {
  3.               trace("Level: "+info.level+"   Code: "+info.code);
  4. };
  5. client_nc.connect("rtmp://200.219.239.68/osfederais/playlist");
  6. rec_so = SharedObject.getRemote("recordings", client_nc.uri, true);
  7. rec_so.onSync = function(list) {
  8.                _root.Play_list.removeAll();
  9.                for (var i in _root.rec_so.data) {
  10.                               _root.Play_list.addItem(i);
  11.                }
  12. };
  13. rec_so.connect(client_nc);
  14. function doRecord() {
  15.                if (ListItem.text == undefined || ListItem.text == "") {
  16.                               Status_msg.text = "Necessario um titulo.";
  17.                               ListItem.setFocus();
  18.                } else if (Record_btn.label == "Record") {
  19.                               Status_msg.text = "Recording...";
  20.                               if (Play_btn.label == "Stop") {
  21.                                              doPlay();
  22.                               }
  23.                               Play_btn.enabled = false;
  24.                               _root.out_ns = new NetStream(_root.client_nc);
  25.                               _root.out_ns.attachAudio(Microphone.get());
  26.                               _root.teste_cam = Camera.get();
  27.                               _root.live.attachVideo(teste_cam);
  28.                               _root.out_ns.attachVideo(_root.teste_cam);
  29.                               _root.out_ns.publish(ListItem.text, "record");
  30.                               _root.rec_so.data[ListItem.text] = ListItem.text;
  31.                               _root.rec_so.flush();
  32.                               Record_btn.label = "Stop";
  33.                } else if (Record_btn.label == "Stop") {
  34.                               _root.out_ns.close();
  35.                               Play_btn.enabled = true;
  36.                               Record_btn.label = "Record";
  37.                               ListItem.text = "";
  38.                               Status_msg.text = "...";
  39.                }
  40. }
  41. function doPlay() {
  42.                if (Play_btn.label == "Play") {
  43.                               Status_msg.text = "Playing...";
  44.                               Play_btn.label = "Stop";
  45.                               var playFileName = Play_list.selectedItem.label;
  46.                               _root.in_ns = new NetStream(_root.client_nc);
  47.                               publicado.attachVideo(_root.in_ns);
  48.                               _root.in_ns.play(playFileName);
  49.                               _root.in_ns.onStatus = function(info) {
  50.                                              if (info.level == "error" || info.code == "NetStream.Play.Stop") {
  51.                                                             Status_msg.text = "parado...";
  52.                                                             Play_btn.label = "Play";
  53.                                              }
  54.                               };
  55.                } else if (Play_btn.label == "Stop") {
  56.                               Status_msg.text = "Stop...";
  57.                               _root.in_ns.onStatus = null;
  58.                               _root.in_ns.close();
  59.                               Play_btn.label = "Play";
  60.                }
  61. }
  62. Play_list.addEventListener("change", cbListener);
  63. var Play_list = _root.createClassObject(mx.controls.List, "Play_list", 1, {_x:357, _y:214});
  64. Play_list.setSize(148, 177);
  65. var Play_btn = _root.createClassObject(mx.controls.Button, "Play_btn", 2, {_x:170, _y:216, label:"Play"});
  66. var Record_btn = _root.createClassObject(mx.controls.Button, "Record_btn", 3, {_x:47, _y:216, label:"Record"});
  67. Record_btn.onRelease = function() {
  68.                doRecord();
  69. };
  70. Play_btn.onRelease = function() {
  71.                doPlay();
  72. };
  73. var cbListener:Object = new Object();
  74. cbListener.change = function(evt_obj:Object) {
  75.                var playFileName = evt_obj.target.selectedItem.label;
  76.                trace(playFileName);
  77.                in_ns.pause();
  78.                in_ns = new NetStream(_root.client_nc);
  79.                publicado.attachVideo(in_ns);
  80.                in_ns.play(playFileName);
  81. };

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/

Flash, Flash Media Server, Principal, RIA

2 comments


  1. how about a playlist for LIVE broadcasts??

  2. you can create a shared object to keep the instances that will be live

Leave a Reply