phasset如何获取视频网址?

时间:2018-04-17 00:46:02

标签: swift url video uicollectionview avplayer

以相机胶卷显示所有视频并在UICollectionView中显示。

phasset如何获取视频网址?

    var videos: PHFetchResult<PHAsset>!

    fileprivate func fetchVideos() {
        print("fetch Videos")
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: false) ]
        fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
        videos = PHAsset.fetchAssets(with: fetchOptions)
        print(videos)
        collectionView?.reloadData()
    }

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.row)

        let asset = videos!.object(at: indexPath.row)
                   ???

    }

CollectionViewCellHeader;

let fileUrl = ""

override init(frame: CGRect) {
    super.init(frame: frame)

    let videoURL = "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"

    let fileUrl = NSURL(string: videoURL) as! URL


    var aPlayer = AVPlayer(url: fileUrl)
    let moviePlayerController = AVPlayerViewController()

    moviePlayerController.player = aPlayer
    moviePlayerController.view.frame = CGRect(x:0, y:0, width:frame.size.width, height:frame.height)
    moviePlayerController.videoGravity = AVLayerVideoGravity.resizeAspectFill.rawValue
    moviePlayerController.view.sizeToFit()
    moviePlayerController.showsPlaybackControls = false
    moviePlayerController.player?.play()
    addSubview(moviePlayerController.view)

}

如何获取所选的视频网址? 如何使用requestPlayerItem()方法?

1 个答案:

答案 0 :(得分:2)

  

如何获取所选视频网址

你没有。你不需要它。 PHImageManager以各种形式直接从PHAsset为您提供视频,具体取决于您要对其进行操作。

例如,如果您的目标是播放视频,请调用此方法:

https://developer.apple.com/documentation/photos/phimagemanager/1616958-requestplayeritem