【问题标题】:Access video using localIdentifier, swift使用 localIdentifier 访问视频,swift
【发布时间】:2016-01-15 17:11:09
【问题描述】:

我想打开一个视频,我有本地标识符。我知道我可以创建这样的 AVPlayer,

let player = AVPlayer(URL: NSURL(fileURLWithPath: path))

是否可以通过提供本地标识符来打开视频?

我将视频用作 PHAsset 来获取其 localIdentifier。可以使用 PHFetchOptions 和 fetchAssetCollectionsWithLocalIdentifiers,但是,它就像一个查询。如果我有本地标识符,是否有更直接的方式来访问视频或图像?

【问题讨论】:

标签: ios swift avplayer phasset photokit


【解决方案1】:

如果您要在 AVPlayer 中展示视频资产,最好从照片中请求提供给播放器的是 AVPlayerItem。假设您在照片库中有视频资产的 localIdentifier,您需要首先找到代表该资产的 PHAsset

let assets = PHAsset.fetchAssetsWithLocalIdentifiers([identifier], options: nil)
guard let asset = assets.firstObject as? PHAsset
    else { fatalError("no asset") }

拥有PHAsset 后,您可以使用PHImageManager 获取资产的视频内容(以AVPlayerItem 的形式):

let options = PHVideoRequestOptions()
options.networkAccessAllowed = true
let requestID = PHImageManager.defaultManager().requestPlayerItemForVideo(asset, 
    options: options, 
    resultHandler: { playerItem, info in
        guard let item = playerItem
            else { fatalError("can't get player item: \(info)") }

        // send the item to whatever you're playing AV content with, e.g.
        myAVPlayerViewController.player = AVPlayer(playerItem: item)
    }
)

【讨论】:

    猜你喜欢
    • 2015-09-20
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2016-05-31
    相关资源
    最近更新 更多