opentok / opentok-ios-sdk-samples-swift

Sample applications using the OpenTok iOS SDK in Swift
https://tokbox.com/
MIT License
137 stars 65 forks source link

What's the equivalent of Session.StreamPropertiesListener on iOS? #215

Open vodemn opened 2 months ago

v-kpheng commented 2 months ago

@goncalocostamendes, any pro tips?

goncalocostamendes commented 2 months ago

@vodemn on iOS you can set up a key-value observer for the properties (hasAudio, hasVideo hasCaptions, etc) of an OTStream. For example to see when a stream has captions enabled and disabled:

swift example

//add observer on a stream of interest
let hasCaptionsObservation: NSKeyValueObservation = subscriber.stream.observe(\.hasCaptions, options: [.old, .new]) { object, change in
    guard let oldValue = change.oldValue else { return }
    guard let newValue = change.newValue else { return }
    print("KVO change for \(subscriber.stream.streamId) hasCaptions: \(newValue)")
    //manipulate GUI element
}

objc example

//add observer on a stream of interest
[_subscriber.stream addObserver:self forKeyPath:@"hasCaptions" options:NSKeyValueObservingOptionNew context:NULL];

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    NSLog(@"KVO change for %@ = %@",keyPath, change); 
    //manipulate GUI element
}