1

我正在尝试使用来自Github的官方 VimeoUpload 库将视频上传到 Vimeo 平台。我已按照此处的安装说明正确设置。

我在这里面临的问题是我没有获得上传进度更新,我正在使用 KVO 方法来获得相同的结果。在注册观察者使用addObserver()并等待一段时间后,执行从未进入observeValue()方法。

以下是我的代码,请检查。

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)
        }
    }

}

4

0 回答 0