Swift

Create the Custom Audio Driver Class

First copy the file OTDefaultAudioDevice.switf to your project. This class handles the heavy lifting of audio routing.

Then, create a new Swift file named AudioDeviceRingtone.swift. We will add logic to manage an AVAudioPlayer.

import OpenTok
import AVFoundation
import AudioToolbox // Required if you want vibration

class AudioDeviceRingtone: OTDefaultAudioDevice, AVAudioPlayerDelegate {
    
    // MARK: - Properties
    private var audioPlayer: AVAudioPlayer?
    private var vibratesWithRingtone: Bool = false
    private var vibrateTimer: Timer?
    private let vibrateFrequencySeconds: TimeInterval = 1.0
    private var ringtoneURL: URL?
    /**
     * This property is only needed later, because we can't always do as requested immediately. 
     * We are defering incoming callbacks from OTAudioBus until we aren't playing anything back
     * (check the sample app for details)
     */
    private var deferredCallbacks: [String] = [] 

    // MARK: - Initialization
    init(ringtone url: URL) {
        self.ringtoneURL = url
        super.init()
    }
    
    // MARK: - Helper: Vibrate Logic
    @objc private func buzz(_ timer: Timer) {
        if vibratesWithRingtone {
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
        }
    }
}

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