Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting videos from Assets catalog using On Demand resources

I attributed the tag "tokyo" to my .mp4 video, and set it as installed during the app installation.

Originally, I was using a path to load it from my resources, however, now it's different because it's located in the Asset catalog.

After reading the documentation, I tried something like:

NSBundleResourceRequest(tags: ["tokyo"]).beginAccessingResourcesWithCompletionHandler { (error) -> Void in
    let tokyoVideo = NSDataAsset(name: "tokyo")
}

To access this video as NSData I could use:

tokyoVideo.data

However, I'm using AVPlayer which takes the parameter as an NSURL, not NSData.

So how do I get the NSURL for my video? Is the Asset catalog only for storing Data and should I be using that to store my video, or is there a better alternative?

like image 710
Aymenworks Avatar asked Jun 22 '15 10:06

Aymenworks


1 Answers

The problem is putting the mp4 in the asset catalogue. Resources don't have to be in the asset catalogue to be accessed as on demand resources.

Move your assets out of the catalogue in to the workspace and tag them then use the bundle property of the NSBundleResourceRequest

import UIKit

class ViewController: UIViewController {
    var bundleRequest = NSBundleResourceRequest(tags: [])

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let tags: Set<String>  = ["odr"]
        bundleRequest = NSBundleResourceRequest(tags: tags)

        bundleRequest.beginAccessingResourcesWithCompletionHandler { (error:NSError?) -> Void in
            if let e = error {
                print(e)
                return
            }
            NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                if let url = self.bundleRequest.bundle.URLForResource("tokyo", withExtension: "mp4") {
                    //use the url to play the video with avplayer
                }

            })
        }
    }

}
like image 86
Peter Lafferty Avatar answered Nov 28 '22 13:11

Peter Lafferty