0

How can we use TapGesture in uicollectionview for playing videos in tvos. When we click the First Image, it should load the appropriate video for that when we tap and so on. How can I do this when the videos are in local ?

import Foundation
import UIKit
class FirstViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate {


let reuseIdentifierFeatured = "FeaturedCell"

var PosterImage : UIImage!
var ImagesDisplay = [String]()
var Try = [AppletvCollectionViewCell]()
var DefaultSize = CGSizeMake(267, 153)
var FocusSize = CGSizeMake(294, 268)

@IBOutlet weak var PosterBackGroundDisplay: UIImageView!

@IBOutlet weak var ScrollView: UIScrollView!

@IBOutlet weak var CollectionView1: UICollectionView!


override func viewDidLoad() {
    super.viewDidLoad()

    ImagesDisplay = ["Slow motion fire video Screenshot-1.jpg","Slow motion fire video Screenshot-2.jpg","Slow motion fire video Screenshot-3.jpg","Slow motion fire video Screenshot-4.jpg","Slow motion fire video Screenshot-5.jpg","Sun Timelapse Screenshot 1.jpg","Sun Timelapse Screenshot 2.jpg"]


    self.CollectionView1.delegate = self

    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
    self.ScrollView!.contentSize = CGSizeMake(1920, 239)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 50
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 50
}

func collectionView(collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0.0, left: 40.0, bottom: 0.0, right: 50.0)
}


func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
        return ImagesDisplay.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    if  let cell : AppletvCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierFeatured, forIndexPath: indexPath) as! AppletvCollectionViewCell {
        cell.Poster.image = UIImage(named: ImagesDisplay[indexPath.row])
        if cell.gestureRecognizers?.count == nil {
            let tap = UITapGestureRecognizer(target: self, action: "Tapped:")
            tap.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
            cell.addGestureRecognizer(tap)
        }

        return cell

    }else {
        return AppletvCollectionViewCell()
    }



}
func Tapped(gesture : UIGestureRecognizer) {

        print("TappedSucess")
        let playerVC = PlayerViewController()
        playerVC.playVideo()
        [self.presentViewController(playerVC, animated: true, completion: nil)]

}

func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    if let prev = context.previouslyFocusedView as? AppletvCollectionViewCell {
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            prev.Poster.frame.size = self.DefaultSize

        })
    }

    if let next = context.nextFocusedView as? AppletvCollectionViewCell {
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            next.Poster.frame.size = self.FocusSize
      self.PosterBackGroundDisplay.image = next.Poster.image
        })
    }

}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let playerVC = PlayerViewController()
    playerVC.playVideo()
    [self.presentViewController(playerVC, animated: true, completion: nil)]
}}
Ajit Medhekar
  • 1,018
  • 1
  • 10
  • 39
  • 1
    Are you sure you need a tap gesture recognizer for that? Typically using `didSelectItemAtIndexPath` delegate methods is used for handling cell selection. – Michał Ciuba Apr 11 '16 at 12:28
  • @michal ciuba yaa didselectitematindexpath is also enough... BUt how cani do that.. –  Apr 11 '16 at 14:43
  • There are many ways to play a video file (no matter if it's local or remote). What have you tried so far and what are the specific problems you had? Please edit your post, add the relevant code (probably part of `PlayerViewController`) and remove the code not related to playing video (layout etc.). This way it will be easier to help you. – Michał Ciuba Apr 11 '16 at 14:47
  • func playVideo() { // if let path = NSBundle.mainBundle().pathForResource("4", ofType:"mp4") { // let url = NSURL(fileURLWithPath: path) // let videoPlayer = AVPlayer(URL: url) // let playerLayer = AVPlayerLayer(player: videoPlayer) // playerLayer.frame = self.view.frame // self.view.layer.addSublayer(playerLayer) // videoPlayer.play() // } } @MichałCiuba –  Apr 12 '16 at 04:11
  • This is the code i am using in playviewcontroller.... But how can i play multiple number of videos.... ?when we click different cell. –  Apr 12 '16 at 04:13
  • Any one help for my question :( –  Apr 12 '16 at 09:27

1 Answers1

1

You can use the given method to load and play video.

func loadAndPlayVideo()
    {
        if let path = NSBundle.mainBundle().pathForResource("LocalFilename", ofType:"mp4")
        {
            let url = NSURL(fileURLWithPath:path)

            let playerItem = AVPlayerItem(URL: url)
            let player = AVPlayer(playerItem: playerItem);

            let playerVC = AVPlayerViewController()
            playerVC.player = player

            self.presentViewController(playerVC, animated: true, completion:nil)
            playerVC.player?.play()
        }
    }

If you want to play multiple video in the same player then use method "replaceCurrentItemWithPlayerItem".

Refer this link for more info- https://iosguy.com/2012/01/11/multiple-video-playback-on-ios/

Meenakshi
  • 1,162
  • 9
  • 13