Swift

Implement the "Play Ringtone" Logic

We need a method that pauses the Vonage audio stream and starts the standard iOS audio player.

Because the iPhone audio system is a global singleton, we cannot play a ringtone and the Vonage stream simultaneously without audio artifacts. Therefore, we stop capturing/rendering Vonage audio before playing the ringtone.

Add this method to your AudioDeviceRingtone class:

func playRingtone(from url: URL) {
    // 1. Pause audio
    // These methods stop the Vonage audio unit from accessing the hardware
    _ = stopCapture()
    _ = stopRendering()
    // 2. Stop & replace existing audio player
    if let player = audioPlayer {
        player.stop()
        audioPlayer = nil
    }
    
    // 3. Initialize the AVAudioPlayer
    do {
        audioPlayer = try AVAudioPlayer(contentsOf: url)
        audioPlayer?.delegate = self
        
        // Loop indefinitely
        audioPlayer?.numberOfLoops = -1
        
        // 4. Setup Vibration Timer
        if vibratesWithRingtone {
            vibrateTimer = Timer.scheduledTimer(timeInterval: vibrateFrequencySeconds,
                                                target: self,
                                                selector: #selector(buzz(_:)),
                                                userInfo: nil,
                                                repeats: true)
        }
        
        // 5. Play the audio
        audioPlayer?.play()
    } catch {
        print("Ringtone audio player initialization failure \(error)")
        audioPlayer = nil
    }
}

Custom audio driver

Learn how to use a custom audio driver to customize publisher and subscriber stream audio. You will use the custom audio driver when you want to start and stop the audio, and play your own audio file. When you want to do "anything" with audio, other than the SDK default behavior of live video chat, you would use custom audio drivers.

Available on:
Swift
Steps
1
Introduction
2
Getting Started
3
Creating a New Project
4
Adding the Vonage Video SDK
5
Setting Up Authentication
6
Overview
7
Create the Custom Audio Driver Class
8
Implement the "Play Ringtone" Logic
9
Implement the "Stop Ringtone" Logic
10
Integrate with Vonage Video Manager
11
Control audio via Session Events
12
How It Works
13
Conclusion