1

New in ios, using Swift 2.0, I have video file in Amazon S3, I use HanekeSwift for download and "cache" that video file and then play it, the issue is that when I play the video, the file written by hakene it isn't yet available, so nothing plays (I'm reusing this player SCPlayer).

How can I get notified when that video file is ready and available for play it?

PS: I've tried using PromiseKit for "play it" in "future" but no luck :(, here the sample of code:

Main call:

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


I used this function as a way to know wich videos has been downloaded, and before play the video check if exists inside self.downloaded 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<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)
        }
    }
}


Verify videos before play it

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


Play the video

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)")
}
user2976753
  • 935
  • 11
  • 24

0 Answers0