I'm trying to upload video on to the Vimeo platform using official VimeoUpload library from Github. I've set it up correctly as per the installation instructions here.
The problem that I'm facing here is I'm not getting the upload progress update, I'm using KVO approach to get the same. After registering observer using addObserver()
and waiting for some time, the execution never come under observeValue()
method.
Below is my code, please check.
import Foundation
import VimeoUpload
protocol VideoUploadListener {
func onVideoUploadUpdate(progress: Double)
func onVideoUploadSuccess(media:PostMedia)
func onVideoUploadFailure()
}
class VimeoHelper:NSObject {
private static let Identifier = "com.ios.weapp"
private static let AccessToken = "my_access_token"
private static let ProgressKeyPath = "progressObservable"
private static let StateKeyPath = "stateObservable"
var postMedia:PostMedia?
private var listener:VideoUploadListener?
private var vimeoUpload:VimeoUploader<OldUploadDescriptor>?
private var descriptor:OldUploadDescriptor?
private var progressKVOContext = UInt8()
private var stateKVOContext = UInt8()
init(postMedia:PostMedia, listener:VideoUploadListener) {
self.postMedia = postMedia
self.listener = listener
}
private func initVimeo() {
vimeoUpload = VimeoUploader<OldUploadDescriptor>(backgroundSessionIdentifier: VimeoHelper.Identifier, accessToken: AccessToken, apiVersion: "3.3.1")
let videoSettings = VideoSettings(title: "Untitled Video", description: "N/A", privacy: "anybody", users: nil, password: nil)
descriptor = OldUploadDescriptor(url: (postMedia?.fileUrl)!, videoSettings: videoSettings)
}
public func startUpload() {
initVimeo()
vimeoUpload?.uploadVideo(descriptor: descriptor!)
descriptor = vimeoUpload?.descriptor(for: VimeoHelper.Identifier)
descriptor?.addObserver(self, forKeyPath: VimeoHelper.ProgressKeyPath, options: .new, context: &self.progressKVOContext)
descriptor?.addObserver(self, forKeyPath: VimeoHelper.StateKeyPath, options: .new, context: &self.stateKVOContext)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let keyPath = keyPath {
switch (keyPath, context) {
case (VimeoHelper.ProgressKeyPath, &self.progressKVOContext):
if let progress = change?[.newKey] as? Double {
//Call Listener
self.listener?.onVideoUploadUpdate(progress: progress)
}
break;
case (VimeoHelper.StateKeyPath, &self.stateKVOContext):
let stateRaw = (change?[.newKey] as? String) ?? DescriptorState.ready.rawValue;
let state = DescriptorState(rawValue: stateRaw)!
//Call Listener
if state == .finished {
self.listener?.onVideoUploadSuccess(media: self.postMedia!)
}
break;
default:
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
} else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
}