1

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

0 回答 0