使用On Demand资源从资产目录中获取视频

Aym*_*rks 4 asset-catalog ios9 xcode7 on-demand-resources

我将.mp4视频归结为"tokyo"标签,并将其设置为在应用安装期间安装.

在我使用Path从我的资源中获取它之前,现在它已经不同了,因为它位于资产目录中.

找到文件后,我尝试了类似的东西:

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

所以我可以这样做:tokyoVideo.data访问NSData,但我使用的是AVPlayer,它接受参数NSURL,而不是数据.

那么你有什么建议我获得我的视频的NSURL?资产目录仅适用于数据吗?所以我不能在那里设置我的视频?

小智 5

问题是将mp4放在资产目录中.资源不必在资产目录中作为按需资源进行访问.

将资产从目录中移出到工作区并标记它们然后使用NSBundleResourceRequest的bundle属性

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
                }

            })
        }
    }

}
Run Code Online (Sandbox Code Playgroud)


Nil*_*tel 0

我认为可以将资产目录用于视频内容,它简化了图像的管理。使用 NSDataAsset 来实现。查看下表中的最后一行。

请参阅此链接以获取更多信息

下表列出了可以标记为按需资源的资源类型。

在此输入图像描述