Skip to main content
LocalAudioTrack represents a local audio track that captures audio from the device’s microphone. It provides methods for muting/unmuting and managing audio capture with configurable audio processing options.

Creating a Track

Create a local audio track using the static factory method:
let audioTrack = LocalAudioTrack.createTrack(
    name: Track.microphoneName,
    options: AudioCaptureOptions(),
    reportStatistics: false
)

createTrack

Static factory method to create a new LocalAudioTrack instance.
name
String
default:"Track.microphoneName"
The name of the audio track
options
AudioCaptureOptions?
default:"nil"
Audio capture configuration options. If nil, default options are used.See AudioCaptureOptions for available settings:
  • echoCancellation: Software echo cancellation
  • autoGainControl: Automatic gain control
  • noiseSuppression: Noise suppression
  • typingNoiseDetection: Typing noise detection
  • highpassFilter: High-pass filter
reportStatistics
Bool
default:"false"
Whether to collect and report statistics for this track
Returns: A new LocalAudioTrack instance configured with the specified options.

Properties

captureOptions

public let captureOptions: AudioCaptureOptions
The AudioCaptureOptions used to create this track. This is a read-only property that reflects the audio processing settings configured during track creation.

publishOptions

public var publishOptions: TrackPublishOptions?
The options used when this track was published, or nil if not yet published.

publishState

public var publishState: Track.PublishState
The current publish state of the track. Can be:
  • .unpublished: Track has not been published
  • .published: Track is currently published

Inherited Properties

From the base Track class:
  • name: The track name
  • sid: The server-assigned track ID (available after publishing)
  • kind: Always .audio for audio tracks
  • source: Always .microphone for local audio tracks
  • isMuted: Whether the track is currently muted
  • trackState: Current state (.started or .stopped)
  • statistics: Real-time statistics if reportStatistics is enabled

Methods

mute()

Mutes the audio track by disabling audio capture.
public func mute() async throws
Muting stops audio transmission without destroying the track. The track remains published but sends silence. Example:
try await audioTrack.mute()

unmute()

Unmutes the audio track by enabling audio capture.
public func unmute() async throws
Example:
try await audioTrack.unmute()

add(audioRenderer:)

Adds an AudioRenderer to receive audio buffers from this track.
public func add(audioRenderer: AudioRenderer)
audioRenderer
AudioRenderer
An object conforming to the AudioRenderer protocol that will receive PCM audio buffers
Audio renderers can be used for visualization, recording, or custom audio processing. Example:
class MyAudioRenderer: AudioRenderer {
    func render(pcmBuffer: AVAudioPCMBuffer) {
        // Process audio buffer
    }
}

let renderer = MyAudioRenderer()
audioTrack.add(audioRenderer: renderer)

remove(audioRenderer:)

Removes a previously added AudioRenderer.
public func remove(audioRenderer: AudioRenderer)
audioRenderer
AudioRenderer
The renderer to remove

start()

Starts audio capture. Inherited from Track.
public func start() async throws
This is typically called automatically when publishing. Manual calls should be balanced with stop() calls.

stop()

Stops audio capture. Inherited from Track.
public func stop() async throws

set(reportStatistics:)

Enables or disables statistics reporting at runtime.
public func set(reportStatistics: Bool) async
reportStatistics
Bool
true to enable statistics collection, false to disable

Track Lifecycle

  1. Creation: Create track using createTrack()
  2. Publishing: Publish track via LocalParticipant.publish(track:options:)
  3. Capturing: Track automatically starts capturing when published
  4. Muting/Unmuting: Use mute() and unmute() to control audio transmission
  5. Unpublishing: Unpublish via LocalParticipant.unpublish(track:)
  6. Cleanup: Track is automatically cleaned up when deallocated

Thread Safety

LocalAudioTrack is @unchecked Sendable and thread-safe. All public methods can be called from any thread, though delegate callbacks are delivered on background threads.

Example Usage

// Create track with custom options
let options = AudioCaptureOptions(
    echoCancellation: true,
    noiseSuppression: true,
    autoGainControl: true
)

let audioTrack = LocalAudioTrack.createTrack(
    name: "my-microphone",
    options: options,
    reportStatistics: true
)

// Publish the track
try await room.localParticipant.publish(track: audioTrack)

// Mute when needed
try await audioTrack.mute()

// Unmute
try await audioTrack.unmute()

// Add audio renderer for visualization
let visualizer = AudioVisualizer()
audioTrack.add(audioRenderer: visualizer)

See Also

Build docs developers (and LLMs) love