Skip to main content
Version: 4.x.x

Mute / Unmute Mic - iOS

This feature allows hosts to communicate with each other during the livestream by enabling or disabling their microphones. While only hosts (participants in SEND_AND_RECV mode) can speak, audience members (in RECV_ONLY mode) can listen to the conversation in real time.

unmuteMic()​

  • By using unmuteMic() function of Room class, the host can publish their audio to other hosts and audience members.

  • You can call this method when the local participant is not broadcasting any audio to others.

  • Audio stream of the participant can be accessed from the onStreamEnabled event of ParticipantEventListener.

muteMic()​

  • By using muteMic() function of Room class, the host can stop publishing their audio to other hosts and audience members.

isMicEnabled​

  • The isMicEnabled property of Room class tells you whether your microphone is on. Before you join, it tells you whether the mic will be turned on at join.
note
  • unmuteMic() and muteMic() are async throws. Call them with try await inside a Task.
  • When a call is refused, it throws a VideoSDKError:
    • ERROR_ACTION_NOT_SUPPORTED_IN_MODE (3066) from unmuteMic() when the participant is in RECV_ONLY mode, as audience members are, or in SIGNALLING_ONLY mode. Only hosts in SEND_AND_RECV mode can turn the mic on.
    • ERROR_ALREADY_IN_REQUESTED_STATE (3054) when the mic is already on, or already off.
    • ERROR_OPERATION_IN_PROGRESS (3055) while an earlier mic call is still running.
    • ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED (3035) before you join the live stream, or after you leave it.
    • ERROR_ROOM_RECONNECTING (3065) while the live stream is reconnecting. The mic stays as it was. Call the method again after it reconnects.
    • ERROR_NO_MICROPHONE_DEVICE_FOUND (3059) when no microphone is available.
  • unmuteMic() returns before the microphone opens. If the microphone then fails to open, for example because the user denied microphone access or another app is using the microphone, the SDK turns the mic off and sends the error only to onError(error:) of RoomEventListener. For example, you get ERROR_MICROPHONE_ACCESS_DENIED_OR_DISMISSED (3018) when the user turns down the permission prompt, and ERROR_MICROPHONE_PERMISSION_DENIED_BY_OS (3020) when microphone access is off in Settings.

Example​

Read isMicEnabled to decide whether to turn the mic on or off.

@IBAction func micButtonTapped(_ sender: Any) {
Task {
do {
guard let room = self.room else { return }
if !room.isMicEnabled {
// enable/unmute mic
try await room.unmuteMic()
} else {
// disable/mute mic
try await room.muteMic()
}
} catch let error as VideoSDKError where error == .ERROR_OPERATION_IN_PROGRESS {
// an earlier tap is still running
} catch {
print("Could not change the mic: \(error)")
}
}
}

Events associated with unmuteMic​

Events associated with muteMic​

Audio Permissions​

  • Step 1: Begin by ensuring that your application has audio permission to access user's microphone device. Utilize the getAudioPermissionStatus() method of the VideoSDK class to check whether permission is granted. It only returns the status. To ask the user, use getAudioPermission() as shown below.
note

In case permissions are blocked by the user, the app's permission request dialogue cannot be re-rendered programmatically. In such cases, consider providing guidance to users on manually adjusting their application settings.

let audioPermission = VideoSDK.getAudioPermissionStatus() // audio permission status

Request Permissions​

  • If permission status is notDetermined, use the getAudioPermission() method of the VideoSDK class to prompt users to grant access to their devices.

  • Calling the above method will automatically handle the notDetermined state and prompt the user to grant permission.

class LiveStreamViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// UI Setup
setupUI()

// prompt user to grant permission when the app starts
VideoSDK.getAudioPermission()
}

func setupUI() {
// your UI code
}
}

API Reference​

The API references for all the methods and events utilized in this guide are provided below.

Got a Question? Ask us on discord