Stop a publisher from streaming
In this guide we will look at stopping publishers from streaming to a session.
To stop a publisher call the unpublish() method of the Session object:
session.unpublish(publisher);
Note that you can individually stop sending video or audio (while still publishing).
For more information, see Adjusting audio and video.
Detecting when a published stream leaves a session
The Publisher object dispatches a streamDestroyed event when it stops streaming to the session:
var publisher = OT.initPublisher();
session.publish(publisher);
publisher.on("streamDestroyed", function (event) {
console.log("The publisher stopped streaming. Reason: "
+ event.reason);
});
The streamDestroyed event is defined by the StreamEvent class. The event includes a reason property, which details why the stream ended. These reasons include "clientDisconnected", "forceDisconnected", "forceUnpublished", or "networkDisconnected". For details, see StreamEvent.
By default, when a Publisher dispatches the streamDestroyed event, the Publisher is destroyed and removed from the HTML DOM. You can prevent this default behavior by calling the preventDefault() method of the StreamEvent object:
publisher.on("streamDestroyed", function (event) {
event.preventDefault();
console.log("The publisher stopped streaming.");
});
You may want to prevent the default behavior, and retain the Publisher, if you want to reuse the Publisher object to publish again to the session.
The Publisher also dispatches a destroyed event when the object has been removed from the HTML DOM. In response to this event, you may choose to adjust (or remove) DOM elements related to the publisher that was removed.
You can stop publisher from streaming to the session by unmounting it (removing it from the parent OTSession component). For example, the following code stops publishing a stream after 30 seconds:
import React, {Component} from 'react';
import {View} from 'react-native';
import {OTSession, OTPublisher} from 'opentok-react-native';
class App extends Component {
constructor(props) {
super(props);
this.apiKey = 'your-api-key';
this.sessionId = 'valid-session-id';
this.token = 'valid-token';
this.publisherOptions = {
publishCaptions: true,
publishVideo: true,
publishAudio: false,
};
this.state = {
publishing: true,
};
this.publisherEventHandlers = {
streamCreated: event => {
setTimeout(
function () {
console.log(10);
this.setState({publishing: false});
}.bind(this),
10000,
);
},
};
}
render() {
return (
<View>
<OTSession
applicationId={this.apiKey}
sessionId={this.sessionId}
token={this.token}
{this.state.publishing ? (
<OTPublisher
eventHandlers={this.publisherEventHandlers}
ref={instance => {
this.publisher = instance;
}}
/>
) : null}
</OTSession>
</View>
);
}
}
export default App;
Note that you can individually stop sending video or audio (while still publishing).
Detecting when a published stream leaves a session
The OTPublisher object dispatches a streamDestroyed event when it stops streaming to the session:
<OTPublisher
eventHandlers={{
streamDestroyed: function() {
console.log('The publisher stopped streaming.');
},
}}
/>
To stop a publisher call the unpublish(PublisherKit publisher) method of the Session object:
mSession.unpublish(mPublisher);
The PublisherKit.PublisherListener.onStreamDestroyed(PublisherKit publisher, Stream stream) method is called when the publisher stops streaming to the session:
@Override
public void onStreamDestroyed(publisher, stream) {
// The publisher stopped streaming.
}
To stop a publisher call the OTSession unpublish(_:error:) method of the OTSession object:
var error: OTError?
session.unpublish(publisher, error: &error)
if let error = error {
print("unpublishing failed with error: \(error)");
}
The OTPublisherDelegate publisher(_:streamDestroyed:) message is sent when the publisher stops streaming to the session. When this message is sent, remove the publisher's view from its superview:
publisher.view?.removeFromSuperview();
To stop a publisher call the [OTSession unpublish:error:] method of the OTSession object:
OTError* error = nil;
[session unpublish:publisher error:&error];
if (error) {
NSLog(@"publishing failed with error: (%@)", error);
}
The [OTPublisherDelegate publisher:streamDestroyed:] message is sent when the publisher stops streaming to the session. When this message is sent, remove the publisher's view from its superview:
[publisher.view removeFromSuperview:];
To stop a publisher call the Unpublish(publisher) method of the Session object:
session.unpublish(mPublisher);
The Publisher sends the StreamCreated event when the publisher starts streaming to the session:
publisher.StreamCreated += Publisher_StreamDestroyed;
session.Publish(publisher);
private void Publisher_StreamDestroyed(object sender, Publisher.StreamEventArgs e)
{
Console.WriteLine("The publisher stopped streaming.");
}
Call the otc_session_unpublish() function, passing in the otc_session and otc_publisher structs:
otc_status status = otc_session_unpublish(session, publisher);
if (status == OTC_SUCCESS) {
printf("Unpublished successfully.");
} else {
printf("Could not unpublish.");
}