Retrieving Video from the Asset Catalog Using On Demand Resources

I assigned my .mp4 video tag tokyo for example and set it as installed during app install.

Now before I used the Path to get it from my resources, it is now different in that it is in the Asset Catalog.

After finding the docs, I tried something like:

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

      

So I can do: tokyoVideo.data to access NSData, but I am using AVPlayer which takes an NSURL parameter, not data.

So what can you suggest me to get the NSURL of my video? Is the Asset Catalog data-only? So I can't install the video?

+3


source to share


3 answers


I think it can be used by Asset Catalog for videos, make it easier to manage. Use NSDataAsset for this. Review the last line in the table below.

Refer to this link for more information



The following table lists the types of resources that can be marked as on-demand resources.

enter image description here

0


source


The problem is the placement of mp4 in the asset directory. Resources do not have to be in the asset catalog to access resources on demand.

Move your assets from catalog to workspace and tag them, then use bundle property for 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
                }

            })
        }
    }

}

      

+4


source


Sure the movie can be saved in a data file.

NSDataAsset

+2


source







All Articles