| Ryerson Home > Ryerson Flash Communication Development Server > Data Playback in NetStream Objects |
The NetStream object can be used to make remote method calls using the send method. For "live" streams this is similar to using the send method of a shared object with some exceptions:
stream_ns.send('showText', input_txt.text);
works fine but you cannot do this:
stream_ns.send('someObject.showText', input_txt.text);
See Macromedia's documentation for more information.
The textPublisher application was designed as a quick test application to experiment with using the NetStream.send method. The interesting part of this is that when a recorded stream is replayed the remote method calls that were stored in the flv file are replayed as well. This test application records one stream named "Demo" so any test recording overwrites the previous test recording. To try this out use the link below to open multiple copies of the Flash movie. In one window use the publish button to start publishing. In the other windows use the play button to play the movie live. Then use the lower text field to type some messages and send them using the Send Text button. You should see the text you typed show up immediately. Stop publishing and then playback the stream using the play button. The text should reappear at the same stream times you pressed the Send Text button. Note: you don't have to accept publishing audio or video to see this work.
Source file:
Thanks to Sam Wan for asking the right question and Giacomo Guilizzoni for answering it. Finally, in case you don't want to download the fla, here is the source listing:
//-----------------------------------------------------------------------------
// Copyright (c) 2002 Ryerson University. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
// This software was written in order to experiment with the Macromedia
// Flash Communication Server MX and is not intended to demonstrate the best
// way to write code for Flash or FlashCom.
//-----------------------------------------------------------------------------
//
// textPublisher.as Version 1.01
// Written by Brian Lesser
// October/2002
// Updated March/2003 to include checking for a full buffer or
// NetStream.Play.Start while checking to see if the stream has
// stopped playing locally.
/**
* writeln writes messages into the trace_txt text field and
* is used in place of trace.
*/
function writeln(msg){
trace_txt.text += msg + "\n";
trace_txt.scroll = trace_txt.maxscroll;
}
/**
* A default onStatus handler for testing. This just
* writes all info.code messages to the text window.
*/
NetStream.prototype.onStatus = function(info){
writeln("NetStream.onStatus> info.code: " + info.code);
}
/**
* This function is assigned to the playing stream onStatus in place
* of the default onStatus handler above.
* It writes all info.code messages to the text window as well
* but also detects when the stream ends and
* changes the button back to "Play" and closes the stream.
* A stream has ended when two things happen in order:
* 1. NetStream.Play.Stop, followed by
* 2. NetStream.Buffer.Empty
*/
NetStreamPlay_onStatus = function(info){
writeln("NetStream.onStatus> info.code: " + info.code);
if (info.code == "NetStream.Play.Stop"){
this.seenStop = true;
}
else if (info.code == "NetStream.Buffer.Empty" && this.seenStop){
this.close();
play_btn.setLabel("Play");
publish_btn.setEnabled(true);
this.seenStop = false;
}
else if (info.code == "NetStream.Play.Start"){
this.seenStop = false;
}
}
// Disable the buttons until a connection is made.
play_btn.setEnabled(false);
publish_btn.setEnabled(false);
// Create a net connection object.
nc = new NetConnection();
// If the connection is made enable the play and publish buttons
// so that streaming can take place.
nc.onStatus = function(info){
// if we connect enable the buttons and say so.
if (info.code == "NetConnection.Connect.Success"){
play_btn.setEnabled(true);
publish_btn.setEnabled(true);
writeln("\n--------------------------------");
writeln("Network connection established. ");
writeln("You can try playing the last recorded stream or record your own over it.");
}
else if(! nc.isConnected){
writeln("\n\nCan't connect or have lost the network connection...");
play_btn.setEnabled(false);
publish_btn.setEnabled(false);
}
}
// Connection to an application instance.
nc.connect('rtmp:/textPublisher');
/**
* doPlay is called when the user presses the play_btn.
* The play button is only enabled when this app is not
* publishing. A net stream is created and given a method
* named showMsg that just writes the text it receives into
* the trace_txt window.
*/
function doPlay(btn){
if (btn.getLabel() == 'Play'){
nss = new NetStream(nc);
nss.showMsg = function (msg){
writeln(msg);
}
nss.onStatus = NetStreamPlay_onStatus;
nss.setBufferTime(10);
vidBox.attachVideo(nss);
publish_btn.setEnabled(false);
btn.setLabel("STOP Playing");
trace_txt.text = "";
nss.play("Demo");
}
else{
nss.close();
btn.setLabel("Play");
publish_btn.setEnabled(true);
}
}
/**
* doPublish is called when the user presses the publish_btn.
* The button is only enabled when this app is not
* playing a stream. A net stream is created and given a method
* named showMsg that just writes the text it receives into
* the trace_txt window. Note, that while a showMsg method is
* defined for the net stream it is never called. Publishers
* do not receive their own send calls back.
*/
function doPublish(btn){
if (btn.getLabel() == "Publish"){
ns = new NetStream(nc);
ns.attachVideo(Camera.get());
ns.attachAudio(Microphone.get());
vidBox.attachVideo(Camera.get());
ns.showMsg = function (msg){
writeln(msg);
}
ns.publish("Demo", 'record');
btn.setLabel("STOP");
play_btn.setEnabled(false);
input_txt.text = "";
}
else{
ns.close();
btn.setLabel("Publish");
play_btn.setEnabled(true);
}
}
function doSendText(){
// Send a message on the publishing stream.
ns.send('showMsg', input_txt.text);
// Try to send a message on the playing stream.
nss.send('showMsg', input_txt.text);
input_txt.text = "";
}
This document was written by Brian Lesser and last updated: . If you find any problems with it or the application please let me know.