AVFoundation 和 UITableView
Posted
技术标签:
【中文标题】AVFoundation 和 UITableView【英文标题】:AVFoundation and UITableView 【发布时间】:2017-06-11 07:20:29 【问题描述】:遇到了问题。我想制作一个由两个表组成的应用程序。第一个表中有一个标签和一个视图,第二个表中有一个按钮和一个视图。我想在两个表的所有标签中播放来自 Internet 的相同视频文件。但是出了点问题……我的错误在哪里?也许有人可以帮助我。
我给了表格不同的标签。并为每个表格定制单元格。
@IBOutlet weak var firstTable: UITableView!
@IBOutlet weak var secondTable: UITableView!
let firstCell = "firstCell"
let secondCell = "secondCell"
let avPlayer = AVPlayer()
var avPlayerLayer: AVPlayerLayer!
override func viewDidLoad()
super.viewDidLoad()
firstTable.delegate = self
firstTable.dataSource = self
secondTable.delegate = self
secondTable.dataSource = self
avPlayerLayer = AVPlayerLayer(player: avPlayer)
view.layer.insertSublayer(avPlayerLayer, at: 0)
let url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")
let playerItem = AVPlayerItem(url: url! as URL)
avPlayer.replaceCurrentItem(with: playerItem)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.viewWillAppear(_:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: avPlayer.currentItem)
override func viewWillAppear(_ animated: Bool)
super.viewWillAppear(animated)
let seconds : Int64 = 0
let preferredTimeScale : Int32 = 1
let seekTime : CMTime = CMTimeMake(seconds, preferredTimeScale)
avPlayer.seek(to: seekTime)
avPlayer.isMuted = true
avPlayer.play()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
if (tableView.tag == 1)
return 30
else if (tableView.tag == 2)
return 20
else
return 0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
if (tableView.tag == 1)
let cell = tableView.dequeueReusableCell(withIdentifier: firstCell, for: indexPath) as! FirstTableViewCell
cell.numCell.text = String(indexPath.row)
viewVideo(viewer: cell.videoCell)
return cell
else
let cell = tableView.dequeueReusableCell(withIdentifier: secondCell, for: indexPath) as! SecondTableViewCell
cell.buttonCell.setTitle(String(indexPath.row), for: .normal)
cell.buttonCell.tag = indexPath.row
cell.buttonCell.addTarget(self, action: #selector(showAlertForRow(sender :)), for: .touchUpInside)
viewVideo(viewer: cell.videoSecondCell)
return cell
func viewVideo(viewer: UIView)
super.viewWillLayoutSubviews()
viewer.layer.addSublayer(avPlayerLayer)
avPlayerLayer.frame.size = CGSize(width: viewer.frame.width, height: viewer.frame.height)
@IBAction func showAlertForRow(sender: UIButton)
let alert = UIAlertController(title:"Test work",message:"Cell at row \(sender.tag) was tapped!",preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
这是我的问题的 gif 吗?
【问题讨论】:
【参考方案1】:我风回答。 AvPlayerLayer 应该在单元格内。但像我一样,所有牢房只有一名球员。
【讨论】:
以上是关于AVFoundation 和 UITableView的主要内容,如果未能解决你的问题,请参考以下文章
iOS 8 和 AVFoundation:如何调暗背景音频?