Call NetConnection.connect() to connect to server A.
About reconnecting streams
You can build applications that support seamless playback when a connection is dropped or when a client switches from a wired to a wireless network connection. For an example of reconnecting streams, see the Stream Reconnect and Smart Seek doc and example hosted by Adobe.
To provide seamless playback, use the ActionScript 3.0 NetStream.attach() method to attach the same NetStream object to a reconnected NetConnection object. You can also use this feature for load balancing.
If the Flash Player version is less than 10.1 or the Adobe Media Server version is less than 3.5.3, the stream closes when the connection drops.
Stream Reconnect ActionScript API
The Stream Reconnect ActionScript API is ActionScript 3.0. These APIs are not supported in ActionScript 2.0.
The following ActionScript 3.0 APIs enable you to reconnect a stream:
NetStream.attach(connection:NetConnection)
Attaches a stream to a NetConnection object.
NetStreamPlayTransitions.RESUME
The RESUME mode causes Flash Player 10.1 and greater to request data from a new connection at the same location where it dropped the previous connection. Flash Player aligns the stream across the two connections so no artifacts or jumps are observed in the video playback. Use this mode when you reconnect a stream that was dropped due to server issues or other connection problems.
NetStreamPlayTransitions.APPEND_AND_WAIT
The APPEND_AND_WAIT mode tells the server to build the playlist but not to stream it. Use this mode to rebuild a playlist after losing a connection and establishing a new connection.
NetConnection.Connect.Closed
Use this event to reconnect a stream, NetConnection.Connect.Closed.
NetConnection.Connect.NetworkChange
Notifies the client that a network connection has changed. Don’t use this event to reconnect a stream, use NetConnection.Connect.Closed.
For detailed information about these APIs, see the ActionScript 3.0 Language Reference.
Using the Stream Reconnect ActionScript API
When a NetConnection closes due to a network change, the stream keeps playing using the existing buffer. Meanwhile, client-side ActionScript code reconnects to the server and resumes playing the stream.
If the Flash Player version is less than 10.1 or the Adobe Media Server version is less than 3.5.3, the stream closes as soon as the network connection is lost.
Reconnecting a single stream
The following workflow reconnects a single stream:
-
-
Create a NetStream. Set NetStream.bufferTime to at least a few seconds so there is enough data to play while the connection is down.
-
Call NetStream.play2() and use NetStreamPlayTransitions.RESET to play a stream called “myStream”.
-
Monitor the NetConnection for the "NetConnection.Connect.Closed" event. Reconnect to server A if the connection drops.
-
Call the NetStream.attach(connection:NetConnection) method to attach the NetStream to the new connection.
-
Call NetStream.play2() and use NetStreamPlayTransitions.RESUME to play the stream “myStream”.
Flash Player tells the server where to resume playing the stream.
-
Do not call NetConnection.close() or NetStream.close() if you intend to reconnect it. Calling close closes and cleans up the connection or the stream, so it will stop playback right away and future reconnects will not be possible.
After a attach is done to the new NetConnection, a close on the older NetConnection can be called.
Reconnecting a playlist
The following workflow reconnects a playlist:
-
Call NetConnection.connect() to connect to server A.
-
Create a NetStream. Set NetStream.bufferTime to at least a few seconds so there is enough data to play while the connection is down.
-
Call NetStream.play2()and use NetStreamPlayTransitions.RESET to play a stream called “myStream1”.
-
Call NetStream.play2() and use NetStreamPlayTransitions.APPEND to play a second stream called “myStream2”.
-
Monitor the NetConnection for the "NetConnection.Connect.Closed" event. Reconnect to server A if the connection drops.
-
Call the NetStream.attach(connection:NetConnection) method to attach the NetStream to the new connection.
-
Call NetStream.play2() and use NetStreamPlayTransitions.APPEND_AND_WAIT to add “myStream1” to the playlist.
-
Call NetStream.play2() and use NetStreamPlayTransitions.RESUME to add “myStream2” to the playlist and resume the stream.
Flash Player tells the server where to resume playing the stream.
Balancing a server load
The following workflow uses the smart reconnect API to balance a load:
-
Call NetStream.attach(connection:NetConnection) to attach a stream to a NetConnection object on another server.
-
After the stream is successfully attached to the new connection, call NetConnection.close() on the prior connection to prevent data leaks.
-
Call NetStream.play2() and use NetStreamPlayOptions.transition to RESUME. Set the rest of the NetStreamPlayOptions properties to the same values you used when you originally called NetStream.play() or NetStream.play2() to start the stream.
Monitoring a network interface change
To monitor changes to the network interface, monitor the NetConnection for the "NetConnection.Connect.NetworkChange" event. This event detects a network change (for example, connecting to a wireless network, disconnecting from a wireless network, disconnecting a network cable, and so on).
Do not use this event to monitor a dropped NetConnection or to write logic for to reconnect a stream. Use "NetConnection.Connect.Closed".
Monitoring a connection on a mobile device
Some mobile devices cannot receive a "NetConnection.Connect.Closed" message. In this case, you can monitor the NetStream.bufferLength and NetStreamInfo.byteCount properties in a timer to discover network issues. When NetStream.bufferLength is less than NetStream.bufferTime, and NetStreamInfo.byteCount is not increasing, there are probably network issues.
netStreamMonitorTimer.start(); netStreamMonitorTimer.addEventListener(TimerEvent.TIMER, timerHandler); lastByteCount = 0; private function timerHandler(e:TimerEvent):void{ if(netstream.bufferLength < netstream.bufferTime && netstream.info.byteCount == lastByteCount) { // Network has issues.. reconnect to a new NetConnection netconnection2 = new NetConnection(); ///on NetConnection.Connect.Success: netStream.attach(netconnection2); } lastByteCount = netstream.info.byteCount; }
Monitoring an RTMPS or RTMPT connection
For RTMPS and RTMPT connections, when you call NetStream.attach(), the "NetConnection.Connect.Closed" event may arrive late or may never arrive.
In this case, monitor the NetStream.bufferLength and NetStreamInfo.byteCount properties in a timer to discover network issues. When NetStream.bufferLength is less than NetStream.bufferTime, and NetStreamInfo.byteCount is not increasing, there are probably network issues.
netStreamMonitorTimer.start(); netStreamMonitorTimer.addEventListener(TimerEvent.TIMER, timerHandler); lastByteCount = 0; private function timerHandler(e:TimerEvent):void{ if(netstream.bufferLength < netstream.bufferTime && netstream.info.byteCount == lastByteCount) { // Network has issues.. reconnect to a new NetConnection netconnection2 = new NetConnection(); ///on NetConnection.Connect.Success: netStream.attach(netconnection2); } lastByteCount = netstream.info.byteCount; }
Authorization plug-in events and properties
Use the E_PLAY event of the Authorization plug-in to control streaming that occurs after a reconnection. The following table summarizes the events and properties for the Stream Reconnect feature:
Property |
Server version |
E_PLAY Notification and Authorization |
F_STREAM_OFFSET |
3.5.3 |
Read-only. |
F_STREAM_TRANSITION |
3.5 |
Read and write. |
There is one new property: F_STREAM_OFFSET. The F_STREAM_OFFSET property indicates where to resume streaming after a reconnection, in seconds.
The F_STREAM_TRANSITION property indicates the transition mode sent by the client in the NetStream.play2() call. The values for Stream Reconnect are “resume” and “appendAndWait”.
For more information about
Server logging
The following events are written to the Adobe Media Server access log for the Stream Reconnect feature:
Event |
Category |
Description |
connect |
session |
A client has connected to a Adobe Media Server application. This event is logged when you re-establish a connection after it drops. |
play |
stream |
A stream has resumed playing. |
stop |
stream |
A stream has stopped playing. |
The following fields are new for the Stream Reconnect feature:
Field |
Description |
x-trans-mode |
The transition mode sent by the client in the NetStream.play2() call. For Stream Reconnect, the transition modes are “resume” and “appendAndWait”. |
x-soffset |
The offset value indicates where to resume streaming after you attach a NetStream. |
These fields are disabled by default. You can optionally display these fields in the authEvent.log and in the access.log.