Новый в iOS, используя Swift 2.0, у меня есть видеофайл в Amazon S3, я использую Hanekeswift для загрузки и «кэшировать» этот видеофайл, а затем воспроизводить его. Проблема в том, что когда я играю в видео, файл, написанный Hakene, еще не доступен, так что ничего не играет (я повторный использование этого игрока Scplayer). < /P>
Как я могу получить уведомление, когда этот видеофайл готов и доступен для воспроизведения? Код: < /p>
Основной вызов: < /p>
// prepare the promise downloads and play first one
when(self.prepareDownloads()).then {
self.playVideo(0)
}
Я использовал эту функцию как способ узнать, какие видео были загружены, и перед воспроизведением видео, чтобы проверить, существует ли она внутри самостоятельно загруженного Dictionary
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 in
return Promise { 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)
}
}
}
< /code>
Проверьте видео перед воспроизведением их < /p>
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])!)
}
}
< /code>
воспроизводить видео < /p>
func playMedia(index: Int, filePath path: NSURL) {
// play the specific video
self.player.setItemByStringPath(path.absoluteString)
// THIS PRINTS "FALSE" CUZ FILE DOESN'T EXISTS YET :/
print(NSFileManager.defaultManager().fileExistsAtPath(path.path!))
self.player.play()
log.debug("playing video \(index)")
}
Подробнее здесь: https://stackoverflow.com/questions/360 ... to-play-it