Display a Message to Subscriber when Publisher Stops Streaming


When the publisher stops publishing, how can the subscriber get that information? When the publisher stops or loses the connection, I want to send a pop-up to the subscribing device. Is there any method from subscriber side? 



There isn't a built-in method, but it is entirely possible with the Android SDK. Here are some tips to implement this yourself.


You would need to assign a R5ConnectionListener to the subscriber stream using the setListener method.


Basing of the publicly available SubscribeTest from the streaming-android repository found here.

1. Define SubscribeTest as a R5ConnectionListener implementation: public class SubscribeTest extends TestDetailFragment implements R5ConnectionListener

2. Add a onConnectionEvent handler method within the class declaration (e.g., above onCreateView)

@Override public void onConnectionEvent(R5ConnectionEvent event) { // `event.name()` will return the enumerated R5ConnectionEvent that occurred // `event.message` will provide any additional messaging }

3. Add the SubscribeTest instance as the listener on the R5Stream instance within the Subscribe method: public void Subscribe() { // ... code already there subscribe = new R5Stream(connection); subscribe.setListener(this); // ... code already there }

By defining the SubscribeTest instance as the R5ConnectionListener in step #3, any events on the stream will be invoked on the onConnectionEvent method. In that handler method, you can find out which event is being invoked using the event.name() method. The returned names correspond to those events enumerated on R5ConnectionEvent, listed here: https://www.red5pro.com/docs/static/android-streaming/enumcom_1_1red5pro_1_1streaming_1_1event_1_1_r5_connection_event.html#ac6d1660970ec39d89951bf37758a1445 Additionally, you can access the optional messaging information on event.message.

4. Handling the event For the issue at hand, you most likely want to detect when:

  • The Playback Stream cannot be started
  • The Playback Stream has stopped due to the Broadcaster stopping

5. Playback Stream cannot be started To listen for error in playback, the event.name() will be equal to R5ConnectionEvent.ERROR:

@Override public void onConnectionEvent(R5ConnectionEvent event) { if (event.name() == R5ConnectionEvent.ERROR.name()) { // handle what is to be shown to the end-user when an error in playback occurs. } }

Most likely the R5ConnectionEvent.ERROR will be thrown when:

  • A stream with the specified name is not available for playback.
  • A connection limit has been reached based on your license type.

6. Playback Stream stopped due to Broadcaster stopping To listen for stop of playback due to stop of broadcast, the event.name() will be equal to R5ConnectionEvent.NET_STATUS and the event.message will be "NetStream.Play.UnpublishNotify:

@Override public void onConnectionEvent(R5ConnectionEvent event) { if (event.name() == R5ConnectionEvent.NET_STATUS.name() && event.message = "NetStream.Play.UnpublishNotify") { // handle what is to be shown to the end-user when the publisher has stopped broadcasting. } }

Have more questions? Submit a request


Please sign in to leave a comment.