简体   繁体   中英

how to play video in swift 3 Xcode 8.2.1

I have displayed all local videos in Collection view as image. but now when i click on cell it should play. my code for displaying in collection view cell is given below 在此处输入图片说明 import UIKit import Photos import AVFoundation import AVKit

class VideoViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var imageArrray = [UIImage]()
   // var playerController = AVPlayerViewController()
   // var player:AVPlayer?
  //  var tempVideoFetchresult = PHFetchResult<AnyObject>()


    override func viewDidLoad() {
        //super.viewDidLoad()
        grabVideos()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




    func grabVideos()
    {
        let imgManager = PHImageManager.default()

        let requestOtions = PHImageRequestOptions()
        requestOtions.isSynchronous = true
        requestOtions.deliveryMode = .highQualityFormat
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

        if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions)
        {
        //    tempVideoFetchresult = fetchResult as! PHFetchResult<AnyObject>
            if fetchResult.count > 0
            {
                for i in 0..<fetchResult.count{
                    imgManager.requestImage(for: fetchResult.object(at: i) , targetSize: CGSize(width :200, height : 200), contentMode: .aspectFill, options: requestOtions, resultHandler: {

                        image, error in

                        self.imageArrray.append(image!)

                    })



                }
            }else{
                print("You got no photos")
                self.collectionView?.reloadData()
            }
        }



    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArrray.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! videoCollectionViewCell

        cell.videoImageView.image = imageArrray[indexPath.row]

        //let imageView = cell.viewWithTag(1) as! UIImageView


        // let imageView = cell.contentView.viewWithTag(1) as! UIImageView
        //  imageView.image = imageArrray[indexPath.row]

        return cell
    }


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = collectionView.frame.width/3 - 1
        return CGSize(width: width, height: width)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //play video
              //  self.player = AVPlayer(playerItem: <#T##AVPlayerItem?#>)
    }


}

i got answer

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

    let imgManager = PHImageManager.default()

    let requestOtions = PHVideoRequestOptions()

    requestOtions.deliveryMode = .highQualityFormat
    imgManager.requestPlayerItem(forVideo: tempVideoFetchresult.object(at: indexPath.row) as! PHAsset, options: requestOtions, resultHandler: {
        avplayeritem, error in
        self.player = AVPlayer(playerItem: avplayeritem)
        self.playerController.player = self.player

    })
          //  self.player = AVPlayer(playerItem: <#T##AVPlayerItem?#>)
    self.present(self.playerController,animated: true,completion: {
        self.playerController.player?.play()
    })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM