我们如何使用 tapGesture 在 Tvos 中播放视频

Posted

技术标签:

【中文标题】我们如何使用 tapGesture 在 Tvos 中播放视频【英文标题】:how can we use tapGesture for playing videos in Tvos 【发布时间】:2016-04-11 10:47:58 【问题描述】:

我们如何在uicollectionview 中使用TapGesture 来播放tvos 中的视频。当我们单击第一个图像时,它应该在我们点击时加载相应的视频等等。当视频在本地时,我该怎么做?

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)]

【问题讨论】:

您确定需要点击手势识别器吗?通常使用didSelectItemAtIndexPath 委托方法来处理单元格选择。 @michal ciuba yaa didselectitematindexpath 也足够了......但是我该怎么做...... 播放视频文件的方式有很多种(无论是本地还是远程)。到目前为止,您尝试了什么,遇到的具体问题是什么?请编辑您的帖子,添加相关代码(可能是PlayerViewController 的一部分)并删除与播放视频无关的代码(布局等)。这样会更容易为您提供帮助。 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 这是我在 playviewcontroller 中使用的代码....但是我怎样才能播放多个视频....?当我们点击不同的单元格时。 【参考方案1】:

您可以使用给定的方法来加载和播放视频。

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()
        
    

如果您想在同一个播放器中播放多个视频,请使用“replaceCurrentItemWithPlayerItem”方法。

更多信息请参考此链接- https://iosguy.com/2012/01/11/multiple-video-playback-on-ios/

【讨论】:

以上是关于我们如何使用 tapGesture 在 Tvos 中播放视频的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 和 tvOS 应用程序中使用相同的产品标识符

如何在应用商店中直播 tvOS 应用

如何确保构建使用 v14.x iOS/tvOS 模拟器?

tvOS 14.3 UISearchController:如何锁定显示

如何在 tvOS 模拟器中安装应用程序?

如何在 UIView 中为 UIImageView 添加 tapGesture