Suggestions

close search

Add Messaging, Voice, and Authentication to your apps with Vonage Communications APIs

Visit the Vonage API Developer Portal

Customizing the UI — iOS (Swift)

These are the adjustments you can make to customize the user interface of OpenTok videos:

Adding a name for a published stream

To specify the name property of the published stream, set the name property of the OTPublisherSettings object you use when initializing a Publisher:

let publisherSettings = OTPublisherSettings()
publisherSettings.name = "Juan"
let publisher = OTPublisher(delegate: self, settings: publisherSettings)

The OTStream class has a name property. When you subscribe to a stream, you can display this name in a user interface element. (Also, this name is displayed when the video in a web page that uses the OpenTok.js library.)

Adding a mute button for a publisher

There is no default user interface element to mute the publisher's microphone. However, you can add an element, such as a button, that sets the publishAudio property of the OTPublisher object when the user clicks it. Set the publishAudio property to false to mute the publisher:

publisher.publishAudio = false

Set the publishAudio property to true to publish audio.

publisher.publishAudio = true

For an example, see the "Project 4: Overlay Graphics" sample in the samples directory of the OpenTok iOS SDK (or at the opentok-ios-sdk-samples repo on github).

Adding a mute button for a subscriber

There is no default user interface element to mute the subscriber's audio. However, you can add an element, such as a button, that sets the subscribeToAudio of the OTSubscriber object when the user clicks it. Set the subscribeToAudio property to false to mute the subscriber

subscriber.subscribeToAudio = false

Set the subscribeToAudio property to true to subscribe to audio (if there is an audio stream):

subscriber.subscribeToAudio = true

For an example, see the "Project 4: Overlay Graphics" sample in the samples directory of the OpenTok iOS SDK (or at the opentok-ios-sdk-samples repo on github).

Adding a button to toggle the publisher's camera

There is no default user interface element to toggle the camera used by the publisher. However, you can add an element, such as a button, that sets the cameraPosition property the OTPublisher object. Set the property to a value defined in the AVCaptureDevicePosition enum. For example, the following code sets the publisher to use the back camera:

publisher.cameraPosition = .back

Note that the cameraPosition property is not available in the OTPublisherKit class. If you are using the OTPublisherKit class to implement a custom video capturer, you can define the camera used in the custom video capturing code.

For an example, see the "Project 4: Overlay Graphics" sample in the samples directory of the OpenTok iOS SDK (or at the opentok-ios-sdk-samples repo on github).

Adjusting user interface when video is enabled or disabled

When a subscriber's video is disabled, the OTSubscriberKitDelegate subscriberVideoDisabled(_:reason:) message is sent. When this occurs, you can add a user interface element (such as an icon) to indicate that the video was disabled:

func subscriberVideoDisabled(_ subscriber: OTSubscriberKit, reason: OTSubscriberVideoEventReason) {
    // Display the video disabled indicator
}

When a subscriber's video is reenabled, the OTSubscriberKitDelegate subscriberVideoEnabled(_:reason:) message is sent. When this occurs, you may remove a user interface element (such as an icon) that indicate that the video is reenabled:

func subscriberVideoEnabled(_ subscriber: OTSubscriberKit, reason: OTSubscriberVideoEventReason) {
    // Remove the video disabled indicator
}

In sessions that use the OpenTok Media Router (sessions with the media mode set to routed), the following messages can also be sent:

You may also want to display and remove a user interface notification (such as an icon) when these messages are sent.

For an example, see the "Project 4: Overlay Graphics" sample in the samples directory of the OpenTok iOS SDK (or at the opentok-ios-sdk-samples repo on github).

Note that when you instantiate a PublisherKit object, you enable or disable publisher and subscriber audio fallback. The audio fallback feature disables video (in publishers and subscribers) when network or CPU conditions do not support video. See Audio fallback (iOS Swift).

If you enable publisher audio fallback, the PublisherKitDelegate object will send the following messages pertaining to publisher audio fallback-related events:

You can display UI indicators in response to these events.

Displaying an indicator element when a session is being archived

When a archive of a session starts recording (or if you connect to a session that is being recorded), the OTSessionDelegate session(_:archiveStartedWithId:name:) message is sent. When the recording stops the OTSessionDelegate session(_:archiveStoppedWithId:) message is sent. You can add a user interface element, such as an icon displayed in a OTPublisher view, to indicate that a video is being recorded:

func session(_ session: OTSession, archiveStartedWithId archiveId: String, name: String?) {
    // Display the archive indicator
}

func session(_ session: OTSession, archiveStoppedWithId archiveId: String) {
    // Hide the archive indicator
}

For an example, see the "Project 5: Multi-Party-Call" sample in the samples directory of the OpenTok iOS SDK (or at the opentok-ios-sdk-samples repo on github).

Getting a snapshot image of a video

The following code captures and display a static image of the Publisher video, adds it to the main view, and sets a UIImage object for the image:

if let publisherView = publisher.view,
	 let screenCapture = publisherView.snapshotView(afterScreenUpdates: true) {
	self.view.addSubview(screenCapture)
	UIGraphicsBeginImageContextWithOptions(publisherView.bounds.size, false, UIScreen.main.scale)
	view.drawHierarchy(in: publisherView.bounds, afterScreenUpdates: true)
	let image = UIGraphicsGetImageFromCurrentImageContext()
	UIGraphicsEndImageContext()
}

The following code captures and display a static image of the subscriber video, adds it to the main view, and sets a UIImage object for the image:

if let subscriberView = subscriber?.view,
	 let screenCapture = subscriberView.snapshotView(afterScreenUpdates: true) {
	self.view.addSubview(screenCapture)
	UIGraphicsBeginImageContextWithOptions(subscriberView.bounds.size, false, UIScreen.main.scale)
	view.drawHierarchy(in: subscriberView.bounds, afterScreenUpdates: true)
	let image = UIGraphicsGetImageFromCurrentImageContext()
	UIGraphicsEndImageContext()
}

For an example of obtaining a high-resolution image of a publisher, see the "Live Photo Capture" sample in the OpenTok iOS Samples repo. This sample is also in the samples directory of the OpenTok iOS SDK.

Adjusting user interface based on audio levels

The OTPublisherKitAudioLevelDelegate publisher(_:audioLevelUpdated:) and OTSubscriberKitAudioLevelDelegate subscriber(_:audioLevelUpdated:) messages are sent on a regular interval with the audio level of the subscriber and publisher. You can use the audioLevel value to update the display in an audio level meter.

For an example, see the "Project 6: Audio Levels" sample in the samples directory of the OpenTok iOS SDK (or at the opentok-ios-sdk-samples repo on github).

Note: A publisher's audio level is not available until its stream is published to a session. To get a preview of the audio before the publisher starts streaming to the session, use a custom audio driver.

Using a custom video renderer

The OTPublisher and OTSubscriber classes provide a default UIView instance that renders the publisher or subscriber video. You can use the custom video stream API to define a custom video renderer, using OTPublisherKit, OTSubscriberKit, and OTVideoRender classes. For an example, see the "Project 2: Let's Build OTPublisher" sample in the samples directory of the OpenTok iOS SDK (or at the opentok-ios-sdk-samples repo on github).

You can also set up a publisher to use a custom video capturer.