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:
Steps
1
Introduction2
Getting Started3
Creating a New Project4
Adding the Vonage Video SDK5
Setting Up Authentication6
Overview7
Create the Custom Audio Driver Class8
Implement the "Play Ringtone" Logic9
Implement the "Stop Ringtone" Logic10
Integrate with Vonage Video Manager11
Control audio via Session Events12
How It Works13
Conclusion