播放时视频文件尚未就绪

时间:2016-03-15 16:36:31

标签: ios swift2 promisekit haneke

新的ios,使用Swift 2.0,我在Amazon S3中有视频文件,我使用HanekeSwift进行下载并“缓存”该视频文件然后播放它,问题是当我播放视频时,由hakene编写的文件尚未提供,因此没有播放(我正在重用此播放器SCPlayer)。

如果该视频文件准备好并可以播放,我该如何收到通知?

PS:我尝试在“未来”中使用PromiseKit来“播放”但没有运气:(这里是代码示例:

主要电话:

// prepare the promise downloads and play first one
when(self.prepareDownloads()).then {
    self.playVideo(0)
}


我使用此功能来了解已下载视频的方式,并在播放视频之前检查self.downloaded词典中是否存在

func prepareDownloads() {

    if let videos = self.videos {

        // iterate the video list for create all the promise
        for (index, video) in videos.enumerate() {
            let promise = { () -> Promise<NSURL> in
                return Promise<NSURL> { fulfill, reject in

                    log.debug("download video \(index)")

                    // request the video and cache it
                    let cache = Shared.dataCache
                    let resource = NSURL(string: (reaction.objectForKey("resourceURL") as! String))!

                    cache.fetch(URL: resource).onSuccess { data in

                        // get the cached file
                        let location = NSURL(string: DiskCache.basePath())!.URLByAppendingPathComponent("shared-data/original")
                        let diskCache = DiskCache(path: location.absoluteString)
                        let file = NSURL(fileURLWithPath: diskCache.pathForKey(resource.absoluteString))

                        self.downloaded.updateValue(file, forKey: index)

                        fulfill(file)
                    }.onFailure { _ in
                        log.error("error downloading video")
                    }
                }
            }

            // save it in dictionary
            self.downloads.updateValue(promise, forKey: index)
        }
    }
}


播放前验证视频

func playVideo(index: Int) {

    // hasn't been downloaded?
    if (self.downloaded[index] == nil) {

        // call the promise to download and then play
        if let promise = self.downloads[index] {
            promise().then { path in

                // play video
                self.playMedia(index, filePath: path)
            }
        }
    } else {

        // the video has been downloaded
        self.playMedia(index, filePath: (self.downloaded[index])!)
    }
}


播放视频

func playMedia(index: Int, filePath path: NSURL) {

    // play the specific video
    self.player.setItemByStringPath(path.absoluteString)

    // THIS PRINT "FALSE" CUZ FILE DONT EXISTS YET :/
    print(NSFileManager.defaultManager().fileExistsAtPath(path.path!))
    self.player.play()

    log.debug("playing video \(index)")
}

0 个答案:

没有答案