Search code examples
iosswiftopentoktokbox

OpentTok (iOS) How to subscribe to hasAudio stream changes


I need a possibility in the TokBox iOS SDK to find out if a subscribed stream has audio dynamically / via an event. (OTStream.hasAudio)

OTSubscriberDelegate provides callbacks for the subscribed video state subscriberVideoEnabled/Disabled but I can't find anything for audio.


Solution

  • TokBox Developer Evangelist here.

    For the iOS SDK, we don't provide a delegate for stream property changes, but you can implement key value observing(KVO) to check if the observed stream property has changed.

    In the example below, I added an observer for the hasAudio stream property and the observer will print the old and new values whenever it changes.

     func session(_ session: OTSession, streamCreated stream: OTStream) {
       let hasAudioObservation: NSKeyValueObservation = stream.observe(\.hasAudio, options: [.old, .new]) { object, change in
         guard let oldValue = change.oldValue else { return }
         guard let newValue = change.newValue else { return }
         print("Old stream value: \(oldValue)")
         print("New stream value: \(newValue)")
    }