Getting video from Asset Catalog using On Demand r

2019-05-05 19:15发布

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

Now before I was using a Path to get it from my resources, now it's different because it's located on the Asset Catalog.

After found documentations, 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'm using AVPlayer which take in parameter an NSURL, not data.

So what can you suggest me for getting a NSURL of my video ? Is the Asset Catalog only for Data ? So I can't set my video there ?

3条回答
聊天终结者
2楼-- · 2019-05-05 19:47

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
                }

            })
        }
    }

}
查看更多
地球回转人心会变
3楼-- · 2019-05-05 19:48

I think its possible to use Asset Catalog for video stuff, Its simplify management of images. Use NSDataAsset for it. Review the last row in below table.

Refer this link for more info

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

enter image description here

查看更多
倾城 Initia
4楼-- · 2019-05-05 20:01

Sure it can the movie will end up getting stored in a data file.

NSDataAsset

查看更多
登录 后发表回答