swift - 我们如何使用 TapGesture 在 Tvos 中播放视频

标签 swift swift2 tvos tvos9.1

我们如何在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)]
}}

最佳答案

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

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/

关于swift - 我们如何使用 TapGesture 在 Tvos 中播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36546543/

相关文章:

swift - 带有 SwiftUI 的 tvOS 上的 NavigationLink 按钮不起作用

swift - tvOS:导航时出现问题

ios - Popover 在 iPhone 上不起作用

ios - 用 Objective C 编写的成功和失败 block ,在 swift 中显示错误

ios - XCode8 Swift3 - 打开联系人列表并通过单击检索数据时出现问题

swift - 如果一个类实现了声明一个可失败初始化器的协议(protocol),为什么它不必提供可失败初始化器?

swift - 为 Alamofire 和 Argo 创建响应式扩展

swift - 如何快速为 alamofire 请求编写通用包装器?

swift - 快速增加年份设置日历单位月份

audio - 可以使用 tvOS 录制音频吗?