YouTube player opening unnecessarily during scrolling of CollectionView - ios

I am working on a chatbot where the different type of response comes from the server and I display the response using UICollectionView cells in chat screen. Different type of cells presents according to server response. when server response with playing video, I am presenting the cell that contains youtube player. I am using https://github.com/kieuquangloc147/YouTubePlayer-Swift. The issue is when I scroll chat screen (collectionView) youtube player is opening again and again. Sometimes it is blocking all the UI element and stop scrolling. I tried different methods but can't able to resolve it. Here is the code:
PlayerView:
import UIKit
class PlayerView: UIView, YouTubePlayerDelegate {
override init(frame: CGRect) {
super.init(frame: frame)
addYotubePlayer()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// youtube player
lazy var youtubePlayer: YouTubePlayerView = {
let viewFrame = UIScreen.main.bounds
let player = YouTubePlayerView(frame: CGRect(x: 0, y: 0, width: viewFrame.width - 16, height: viewFrame.height * 1/3))
player.delegate = self
return player
}()
// used as an overlay to dismiss the youtube player
let blackView = UIView()
// youtube player loader
lazy var playerIndicator: UIActivityIndicatorView = {
let indicator = UIActivityIndicatorView()
indicator.activityIndicatorViewStyle = .whiteLarge
indicator.hidesWhenStopped = true
return indicator
}()
// shows youtube player
func addYotubePlayer() {
if let window = UIApplication.shared.keyWindow {
blackView.frame = window.frame
self.addSubview(blackView)
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
let tap = UITapGestureRecognizer(target: self, action: #selector(handleDismiss))
tap.numberOfTapsRequired = 1
tap.cancelsTouchesInView = false
blackView.addGestureRecognizer(tap)
let centerX = UIScreen.main.bounds.size.width / 2
let centerY = UIScreen.main.bounds.size.height / 2
blackView.addSubview(playerIndicator)
playerIndicator.center = CGPoint(x: centerX, y: centerY)
playerIndicator.startAnimating()
blackView.addSubview(youtubePlayer)
youtubePlayer.center = CGPoint(x: centerX, y: centerY)
blackView.alpha = 0
youtubePlayer.alpha = 0
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 1
self.youtubePlayer.alpha = 1
}, completion: nil)
}
}
func play(_ videoID: String) {
youtubePlayer.loadVideoID(videoID)
}
#objc func handleDismiss() {
blackView.removeFromSuperview()
UIApplication.shared.keyWindow?.viewWithTag(24)?.removeFromSuperview()
UIApplication.shared.keyWindow?.removeFromSuperview()
}
func playerReady(_ videoPlayer: YouTubePlayerView) {
self.playerIndicator.stopAnimating()
}
func playerStateChanged(_ videoPlayer: YouTubePlayerView, playerState: YouTubePlayerState) {
}
func playerQualityChanged(_ videoPlayer: YouTubePlayerView, playbackQuality: YouTubePlaybackQuality) {
}
}
YouTubePlayerCell (Which I present in collectionView wthe hen server responds for video):
import UIKit
class YouTubePlayerCell: ChatMessageCell {
var player: PlayerView = PlayerView(frame: UIScreen.main.bounds)
override func setupViews() {
super.setupViews()
setupCell()
}
func setupCell() {
messageTextView.frame = CGRect.zero
textBubbleView.frame = CGRect.zero
}
func loadVideo(with videoID: String) {
player.tag = 24
UIApplication.shared.keyWindow?.addSubview(player)
player.play(videoID)
}
override func prepareForReuse() {
super.prepareForReuse()
player.removeFromSuperview()
UIApplication.shared.keyWindow?.viewWithTag(24)?.removeFromSuperview()
}
}
Here is how I am presenting the YouTubePlayerCell in cellForItemAt method of UICollectionView
let message = messages[indexPath.row]
if message.actionType == ActionType.video_play.rawValue {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ControllerConstants.youtubePlayerCell, for: indexPath) as? YouTubePlayerCell {
self.resignResponders()
if let videoId = message.videoData?.identifier {
cell.loadVideo(with: videoId)
}
return cell
}
}
Full Source Code can be found here: https://github.com/imjog/susi_iOS/tree/ytplayer

I can see that in the below code
if let videoId = message.videoData?.identifier {
cell.loadVideo(with: videoId)
}
you are calling loadVideo method, which is responsible for showing the player.
So while scrolling you are reusing the cell and it calls loadVideo method and present the player. so the solution is don't start playing the video by default on displaying the cell, provide a play/pause button on the cell video overlay and on clicking the the button start playing the video.
If my analysis is wrong please let me know, what exact issue you have.

Why do you add the player as a subView each time you have to play the video ? My suggestion would be, as you are adding the player view on the whole screen, you can have just one instance of the view and add it just once(may be at the beginning) and keep it hidden. To play the video just unhide the player and load the video.
Instead best practice would be to have a View controller for Youtube Player and present it with the video id each time you need to play and then dismissing when done.

Thanks for your answers. I solve this by this way:
Rather than presenting Player on setting on the cell, I am now adding a thumbnail to the cell and a button on thumbnail view so that whenever the user clicks play button, it opens a new controller (Previously I was presenting in UIWindow) and presenting it as modalPresentationStyle of overFullScreen by using protocol because cell cannot present a ViewController.
Protocol: (In YouTubePlayerCell class)
protocol PresentControllerDelegate: class {
func loadNewScreen(controller: UIViewController) -> Void
}
Final YouTubePlayer.swift:
import UIKit
import Kingfisher
protocol PresentControllerDelegate: class {
func loadNewScreen(controller: UIViewController) -> Void
}
class YouTubePlayerCell: ChatMessageCell {
weak var delegate: PresentControllerDelegate?
var message: Message? {
didSet {
addThumbnail()
}
}
lazy var thumbnailView: UIImageView = {
let imageView = UIImageView()
imageView.image = ControllerConstants.Images.placeholder
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.layer.cornerRadius = 15
imageView.isUserInteractionEnabled = true
return imageView
}()
lazy var playButton: UIButton = {
let button = UIButton(type: .system)
button.setImage(ControllerConstants.Images.youtubePlayButton, for: .normal)
button.addTarget(self, action: #selector(playVideo), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func setupViews() {
super.setupViews()
setupCell()
prepareForReuse()
}
func setupCell() {
messageTextView.frame = CGRect.zero
textBubbleView.frame = CGRect(x: 8, y: 0, width: 208, height: 158)
textBubbleView.layer.borderWidth = 0.2
textBubbleView.backgroundColor = .white
}
override func prepareForReuse() {
super.prepareForReuse()
thumbnailView.image = nil
}
func addThumbnail() {
textBubbleView.addSubview(thumbnailView)
textBubbleView.addConstraintsWithFormat(format: "H:|-4-[v0]-4-|", views: thumbnailView)
textBubbleView.addConstraintsWithFormat(format: "V:|-4-[v0]-4-|", views: thumbnailView)
self.downloadThumbnail()
self.addPlayButton()
}
func addPlayButton() {
thumbnailView.addSubview(playButton)
playButton.heightAnchor.constraint(equalToConstant: 44).isActive = true
playButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
playButton.centerXAnchor.constraint(equalTo: thumbnailView.centerXAnchor).isActive = true
playButton.centerYAnchor.constraint(equalTo: thumbnailView.centerYAnchor).isActive = true
}
func downloadThumbnail() {
if let videoID = message?.videoData?.identifier {
let thumbnailURLString = "https://img.youtube.com/vi/\(videoID)/default.jpg"
let thumbnailURL = URL(string: thumbnailURLString)
thumbnailView.kf.setImage(with: thumbnailURL, placeholder: ControllerConstants.Images.placeholder, options: nil, progressBlock: nil, completionHandler: nil)
}
}
#objc func playVideo() {
if let videoID = message?.videoData?.identifier {
let playerVC = PlayerViewController(videoID: videoID)
playerVC.modalPresentationStyle = .overFullScreen
delegate?.loadNewScreen(controller: playerVC)
}
}
}
Delegate implementation in CollectionViewController:
extension ChatViewController: PresentControllerDelegate {
func loadNewScreen(controller: UIViewController) {
self.present(controller, animated: true, completion: nil)
}
}
Final source code can be found here: https://github.com/fossasia/susi_iOS/pull/372

Related

How to build a context menu like Facebook / Slack on iOS?

I was just looking at Context menu of Facebook and or slack and wanted to create something similar in my App.
I have tried two methods.
First method. Having a in View Table View and sliding it from bottom to create as if it is animated on to the view. But the problem with this is that The navigation controller and Tab bar controller are not hidden and a white patch is shown over the Black (Alpha 30 %).
The second method I tried was showing a new View controller over the current view controller and presenting as a Modal presentation.
let vc = CustomActionTableViewController(nibName: "CustomActionTableViewController", bundle: nil)
vc.modalPresentationStyle = .overFullScreen
self.present(vc, animated: false, completion: nil)
This works okay but the method is too slow as I have to work with lot of Notifications (To send selected index to my main View and then perform action). It is painfully slow.
Could anyone help me with how I can improve the implementation so that I can get the Action sheet similar to Facebook which is smooth and very very fluid
Check this example : Bottom pop Up
Currently I am using this in my app and it's work fine.
Since you mentioned Slack, they actually have open sourced their bottom sheet implementation, PanModal.
Using UIPresentationController and UIPanGestureRecognizer
1- create BottomMenu presentation Controller which will handle the height of your View Controller and blur
class BottomMenuPresentationController: UIPresentationController {
// MARK: - Properties
var blurEffectView: UIVisualEffectView?
var tapGestureRecognizer = UITapGestureRecognizer()
private var topHeightRatio: Float
private var bottomHeightRatio: Float
init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?, topHeightRatio: Float, bottomHeightRatio: Float) {
let blurEffect = UIBlurEffect(style: .systemThickMaterialDark)
blurEffectView = UIVisualEffectView(effect: blurEffect)
self.topHeightRatio = topHeightRatio
self.bottomHeightRatio = bottomHeightRatio
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
blurEffectView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissController))
self.blurEffectView?.isUserInteractionEnabled = true
self.blurEffectView?.addGestureRecognizer(tapGestureRecognizer)
}
override var frameOfPresentedViewInContainerView: CGRect {
CGRect(origin: CGPoint(x: 0, y: self.containerView!.frame.height * CGFloat(topHeightRatio)),
size: CGSize(width: self.containerView!.frame.width, height: self.containerView!.frame.height * CGFloat(bottomHeightRatio)))
}
override func presentationTransitionWillBegin() {
self.blurEffectView?.alpha = 0
if let blurEffectView = blurEffectView {
self.containerView?.addSubview(blurEffectView)
}
self.presentedViewController.transitionCoordinator?.animate(alongsideTransition: { (_) in
self.blurEffectView?.alpha = 0.66
}, completion: { (_) in })
}
override func dismissalTransitionWillBegin() {
self.presentedViewController.transitionCoordinator?.animate(alongsideTransition: { (_) in
self.blurEffectView?.alpha = 0
}, completion: { (_) in
self.blurEffectView?.removeFromSuperview()
})
}
override func containerViewWillLayoutSubviews() {
super.containerViewWillLayoutSubviews()
presentedView!.roundCorners([.topLeft, .topRight], radius: 14)
}
override func containerViewDidLayoutSubviews() {
super.containerViewDidLayoutSubviews()
presentedView?.frame = frameOfPresentedViewInContainerView
blurEffectView?.frame = containerView!.bounds
}
#objc func dismissController() {
self.presentedViewController.dismiss(animated: true, completion: nil)
}
}
2- create Your ViewController
class BottomMenuVC: UIViewController {
// MARK: - Instances
var hasSetPointOrigin = false
var pointOrigin: CGPoint?
// MARK: - Properties
let topDarkLine: UIView = {
let view = UIView()
view.backgroundColor = UIColor(hexString: "#E1E1E1")
view.layer.cornerRadius = 2
return view
}()
let cancelButn: UIButton = {
let button = UIButton(type: .custom)
button.setAttributedTitle(NSAttributedString(string: "Cancel", attributes: [NSAttributedString.Key.font: UIFont.LatoMedium(size: 17),
NSAttributedString.Key.foregroundColor: UIColor(hexString: "#515151")
]), for: .normal)
button.backgroundColor = UIColor(hexString: "#F1F3F4")
button.layer.cornerRadius = 5.0
button.addTarget(self, action: #selector(cancelButnPressed), for: .touchUpInside)
return button
}()
// MARK: - viewLifeCycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.isUserInteractionEnabled = true
setupMenuView()
}
override func viewDidLayoutSubviews() {
if !hasSetPointOrigin {
hasSetPointOrigin = true
pointOrigin = self.view.frame.origin
}
}
// MARK: - SetupView
func setupMenuView() {
self.view.addSubview(topDarkLine)
self.view.addSubview(cancelButn)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognizerAction(_:)))
view.addGestureRecognizer(panGesture)
topDarkLine.constrainHeight(constant: 4)
topDarkLine.constrainWidth(constant: view.frame.size.width * 0.10)
topDarkLine.centerXInSuperview()
topDarkLine.anchor(top: view.topAnchor, leading: nil, bottom: nil, trailing: nil, padding: .init(top: 8, left: 0, bottom: 0, right: 0))
cancelButn.anchor(top:view.topAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor,
padding: .init(top: 16, left: 16, bottom: 0, right: 16))
cancelButn.constrainHeight(constant: 44)
}
// MARK: - Actions
#objc func panGestureRecognizerAction(_ sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: view)
// Not allowing the user to drag the view upward
guard translation.y >= 0 else { return }
// setting x as 0 because we don't want users to move the frame side ways!! Only want straight up or down in the y-axis
view.frame.origin = CGPoint(x: 0, y: self.pointOrigin!.y + translation.y)
if sender.state == .ended {
let dragVelocity = sender.velocity(in: view)
if dragVelocity.y >= 1300 {
// Velocity fast enough to dismiss the uiview
self.dismiss(animated: true, completion: nil)
} else {
// If the dragging isn’t too fast, resetting the view back to it’s original point
UIView.animate(withDuration: 0.3) {
self.view.frame.origin = self.pointOrigin ?? CGPoint(x: 0, y: 400)
}
}
}
}
#objc func cancelButnPressed() {
dismiss(animated: true, completion: nil)
}
}
3- make the viewController that contain the button that will present your menu conforms to UIViewControllerTransitioningDelegate
extension viewController: UIViewControllerTransitioningDelegate {
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
BottomMenuPresentationController(presentedViewController: presented, presenting: presenting, topHeightRatio: 0.6, bottomHeightRatio: 0.4)
}
}
4- set the transitioning delegate to self and present your custom presentation Controller
func showBottomMenu() {
let menu = BottomMenuVC()
menu.coordinator = self
menu.modalPresentationStyle = .custom
menu.transitioningDelegate = self
present(menu, animated: true, completion: nil)
}
check this PanGesture Slidable View article

tapgesture not working in collectionViewCell, Neither any UIElements is visible on cell which is added by storyboard

I have a collectionViewCell that either plays a video or displays and image.Now the elements in the cell are generated programatically. I added tap gesture to toggle the sound when video play. The gesture recognizer wasn't getting called. I tried to place a button in story and get its action, that also didn't recieve a call. Then, I tried to place a view inside the cell, that also didn't display.
Here is my code with tap gesture:
import UIKit
import AVKit
import AVFoundation
#IBDesignable class CHCollectionImageCell: UICollectionViewCell {
// MARK: Properties
var imgView: UIImageView! = UIImageView()
var screenWidth:CGFloat = 0
var screenHeight:CGFloat = 0
var playerLayer: AVPlayerLayer!
let tapOnCell = UITapGestureRecognizer(target: self, action: #selector (CHCollectionImageCell.changeMuteRegimeVideo))
// MARK: Functions
override func awakeFromNib() {
super.awakeFromNib()
}
func configureCell(insight: InsightModel) {
imgView.removeFromSuperview()
if playerLayer != nil {
playerLayer.removeFromSuperlayer()
playerLayer = nil
}
self.removeGestureRecognizer(tapOnCell)
if insight.isVideo {
guard let unwrappedVideoURLString = insight.videoURL,
let unwrappedVideoURL = URL(string: unwrappedVideoURLString) else {
return
}
let playerItem = AVPlayerItem(url: unwrappedVideoURL)
let player = AVPlayer(playerItem: playerItem)
playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.bounds
player.isMuted = false
layer.addSublayer(playerLayer)
addGestureRecognizer(self.tapOnCell)
} else {
imgView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.width)
imgView.image = UIImage(named: "stone")
imgView.contentMode = UIViewContentMode.scaleAspectFill
imgView.clipsToBounds = true
clipsToBounds = true
addSubview(self.imgView)
}
}
/*
#IBAction func tapToTurnOfSound(_ sender: Any) {
if isInsightViedo{
if let unwrappedPlayer = playerLayer.player {
unwrappedPlayer.isMuted = !unwrappedPlayer.isMuted
}
}
//Even Tried adding view as below in the cell
//let tapView = UIView()
//tapView.backgroundColor = ColorCodes.appThemeColor
//self.addSubview(tapView)
//self.bringSubview(toFront: tapView)
//tapView.addGestureRecognizer(tapOnCell)
}
*/
func configureCell() {
imgView.removeFromSuperview()
if playerLayer != nil {
playerLayer.removeFromSuperlayer()
playerLayer = nil
}
self.removeGestureRecognizer(tapOnCell)
imgView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.width)
imgView.image = UIImage(named: "stone")
imgView.contentMode = UIViewContentMode.scaleAspectFill
imgView.clipsToBounds = true
clipsToBounds = true
addSubview(self.imgView)
}
func changeMuteRegimeVideo() {
if let unwrappedPlayer = playerLayer.player {
unwrappedPlayer.isMuted = !unwrappedPlayer.isMuted
}
}
}
Iam doing the same thing in my application by using the following code :
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(viewController.longPress(_:)))
longPressGesture.minimumPressDuration = 0.8
longPressGesture.delegate = self
collectionView.addGestureRecognizer(longPressGesture)
and then call the function:
func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
let touchPoint = longPressGestureRecognizer.location(in: collectionView)
if eventsTableView.indexPathForRow(at: touchPoint) != nil {
let index = eventsTableView.indexPathForRow(at: touchPoint)//do whatever you want to do with this index
}}}
you can do whatever you want to do in this function. In my case i used this to enlarge the image in the collection view

Getting error Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) when loading AVPlayer

I am trying to load an AVPlayer when I select a collectionViewCell, Here is my code in didSelectItem :
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let item = items?[indexPath.item] {
showPlayer(item: item)
}
}
func showPlayer(item: Item) {
let playerLauncher = PlayerLauncher()
playerLauncher.showVideoPlayer()
let playerView = PlayerView()
playerView.item = item
playerView.setupPlayerView()
}
This is my PlayerLauncher file:
class PlayerView: UIView {
var item: Item? {
didSet {
if let id = item?.itemId {
itemId = id
}
}
}
var itemId = String()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor(white: 0.3, alpha: 1)
}
var player: AVPlayer?
func setupPlayerView() {
let baseurl = "http://xample.com/play.m3u?id="
let urlString = "\(baseurl)\(itemId)"
print(urlString)
if let url = URL(string: urlString) {
player = AVPlayer(url: url)
let playerLayer = AVPlayerLayer(player: player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.frame
let audioSession = AVAudioSession.sharedInstance()
do{
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
} catch let err {
print(err)
return
}
player?.play()
player?.addObserver(self, forKeyPath: "currentItem.status", options: .new, context: nil)
}
}
}
class PlayerLauncher: NSObject {
func showVideoPlayer() {
print("Showing the player...")
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.backgroundColor = UIColor.white
view.frame = CGRect(x: keyWindow.frame.width - 10, y: keyWindow.frame.height - 10, width: 10, height: 10)
let playerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.height)
let playerView = PlayerView(frame: playerFrame)
view.addSubview(playerView)
keyWindow.addSubview(view)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
view.frame = keyWindow.frame
}, completion: { (completedAnimation) in
//later...
})
}
}
}
So whenever I select the item, the player starts loading, console prints the URL (because I've added print statement there), This is what it prints:
http://xample.com/play.m3u?id=12345
(lldb)
and then It crashes and shows Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) error in AppDelegate. How can I fix it?
Thanks.
UPDATE:
I changed the didSelectItem func a bit. Here is the code:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let item = items?[indexPath.item] {
showPlayer(item: item)
}
}
func showPlayer(item: Item) {
let playerLauncher = PlayerLauncher()
playerLauncher.showPlayer(item: item)
}
I changed the init method in PlayerView to this:
var item: Item? {
didSet {
if let id = item?.itemId {
itemId = id
}
}
}
init(frame: CGRect, item: Item) {
super.init(frame: frame)
self.item = item
setupPlayerView()
setupContainerView()
backgroundColor = UIColor(white: 0.3, alpha: 1)
}
And the required changes in PlayerLauncher:
let playerView = PlayerView(frame: playerFrame, item: item)
view.addSubview(playerView)
I put a break on let playerView line and I can see the item Id passed there. But it is not getting passed to the urlString in PlayerView class. Hence, ID is still " ".
I've made a guide that hopefully shows what you've been misunderstanding.
I apologize to other readers for not using MARKDOWN text format, but please do consider this as my upmost attempt for the original poster. We've already went through sufficient discussions in the previous post.
So, how do you fix this?
I guess there's nothing good to give you codes directly, please try to fix the code based on this information.
And if you have more questions, I'll try my best to help you :)
It appears
class PlayerLauncher: NSObject
Has no way to reference
let playerView = PlayerView()
Try to define those outside the function scope so they can be changed by the function and referenced outside the function.

How to get button to display different video according to the row it is pressed in?

Currently I have my code setup to where in each table view cell there is a button that displays a video after is pressed. Each table view cell row contains the button (there are x amount of cells) however no matter which row the button is tapped in it always leads to the same video. Is there a way to make to where, depending on the row the button is in, it displays a video? My code only has one video file in it currently but how could I make it to where depending on the cell the button is tapped in, it shows a specific video? For example, if the button is tapped in row one I want it to show a certain video, and the same for two, and three, and so on. Right now they all display the same video.
Here is my code for the table view cell:
import UIKit
import AVFoundation
import AVKit
class VideoPlayerView: UIView {
let pauseButton: UIButton = {
let button = UIButton(type: .system)
button.setImage(#imageLiteral(resourceName: "Triangle 2"), for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.tintColor = UIColor.white
button.isHidden = false
button.addTarget(self, action: #selector(handlePause), for: .touchUpInside)
return button
}()
var player: AVPlayer?
var isPlaying = false
func handlePause() {
if isPlaying {
player?.pause()
pauseButton.alpha = 1.0 }
else { player?.play()
pauseButton.alpha = 0.01
}
isPlaying = !isPlaying
}
//container view that holds sublayers for the video control objects
let controlsContainerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(white: 0, alpha: 1.0)
return view
}()
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
//setupPlayerView()
//configures container view (video's background)
controlsContainerView.frame = frame
addSubview(controlsContainerView)
backgroundColor = UIColor.black
//following adds pause/play button to video
controlsContainerView.addSubview(pauseButton)
pauseButton.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
pauseButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
}
//function that sets up video playback
private func setupPlayerView() {
//variable that contains video url
let fileUrl = URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/dunk.mov")
player = AVPlayer(url: fileUrl)
//video only renders if you specify 'playerLayer'
let playerLayer = AVPlayerLayer(player: player)
self.layer.insertSublayer(playerLayer, at: 1)
playerLayer.frame = frame
player?.play()
//attached obeserver of 'player' to tell when 'player' is ready
player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)
}
//method called every time you add obserever to an object
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
//strring that lets AVPlayer know its ready
if keyPath == "currentItem.loadedTimeRanges" {
//configures container view while video is playing
controlsContainerView.backgroundColor = UIColor.clear
pauseButton.alpha = 0.05
isPlaying = true
}
}
}
class DrillsTableViewCell: UITableViewCell {
var videoURL:[URL] = [URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/dunk.mov"), URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/mk.MOV")]
var video = URL(fileURLWithPath: String())
#IBOutlet weak var logoImage: UIImageView!
#IBOutlet weak var drillTitle: UILabel!
#IBOutlet weak var playButton: UIButton!
#IBAction func watchButton(_ sender: Any) {
print(123)
//controls video background view
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.backgroundColor = UIColor.white
view.frame = CGRect(x: 0.0, y: 0.0, width: keyWindow.frame.width, height: keyWindow.frame.height)
let videoPlayerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.width * 9 / 16)
let videoPlayerView = VideoPlayerView(frame: videoPlayerFrame)
view.addSubview(videoPlayerView)
keyWindow.addSubview(view)
UIView.animate(
withDuration: 0.5,
delay: 0,
options: .curveEaseOut,
animations: {
view.frame = keyWindow.frame
},
completion: { completedAnimation in
//possible features implemented later
UIApplication.shared.isStatusBarHidden = true
}
)
}
}
}
Code for table view:
class DrillsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var arrayForKey2 = [[String]]()
var keyIndex = Int()
var headLabel = String()
var labels = Array(trainingDict.keys)
#IBOutlet weak var tableView: DrillsTableView!
#IBOutlet weak var drillLabel: UILabel!
#IBOutlet weak var labelBackground: UIView!
#IBAction func back(_ sender: Any) {
performSegue(withIdentifier: "back", sender: self)
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayForKey2.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell" , for: indexPath) as! DrillsTableViewCell
cell.playButton.tag = indexPath.row
//clear background color needed in order to display gradient cell
cell.backgroundColor = UIColor.clear
//gradient configuration
gradient = CAGradientLayer()
gradient.frame = tableView.bounds
gradient.colors = [UIColor.black.cgColor, UIColor.darkGray.cgColor, UIColor.black.cgColor]
tableView.layer.insertSublayer(gradient, at: 0)
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
//Possible method for 'drillLabel' gradient
drillLabel.font = UIFont(name: "Symbol", size: 24.0)
//attributes for watch/play button
cell.playButton.layer.shadowColor = UIColor.black.cgColor
cell.playButton.layer.shadowOffset = CGSize(width: 2, height: 2)
cell.playButton.layer.shadowOpacity = 0.7
cell.playButton.layer.shadowRadius = 1
//details for cell label display
cell.borderWidth = 1.5
cell.borderColor = UIColor.white
cell.drillTitle.text = "\(arrayForKey2[keyIndex][indexPath.row])"
cell.drillTitle.font = UIFont(name: "Symbol", size: 18.0)
cell.drillTitle.textColor = UIColor.white
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
drillLabel.text = labels[keyIndex]
}
}
I believe you should refactor your code to get required behaviour. Please check the following code:
First make changes in VideoPlayerView method named setupPlayerView .Replace your implementation with this:
func setupPlayerView(for url: URL) {
player = AVPlayer(url: url)
//video only renders if you specify 'playerLayer'
let playerLayer = AVPlayerLayer(player: player)
self.layer.insertSublayer(playerLayer, at: 1)
playerLayer.frame = frame
player?.play()
//attached obeserver of 'player' to tell when 'player' is ready
player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)
}
Now make change in DrillsTableViewCell ,make changes in videosURLs and I added a new variable singleVideoURL, your new class will look like this:
class DrillsTableViewCell: UITableViewCell {
var videoURLs:[URL] = [URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/dunk.mov"), URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/mk.MOV")]
#IBOutlet weak var logoImage: UIImageView!
#IBOutlet weak var drillTitle: UILabel!
#IBOutlet weak var playButton: UIButton!
#IBAction func watchButton(_ sender: UIButton) {
print(123)
//controls video background view
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.backgroundColor = UIColor.white
var singleVideoURL = videoURLs[sender.tag]
view.frame = CGRect(x: 0.0, y: 0.0, width: keyWindow.frame.width, height: keyWindow.frame.height)
let videoPlayerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.width * 9 / 16)
let videoPlayerView = VideoPlayerView(frame: videoPlayerFrame)
videoPlayerView .setupPlayerView(for: singleVideoURL)
view.addSubview(videoPlayerView)
keyWindow.addSubview(view)
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
view.frame = keyWindow.frame
}, completion: { (completedAnimation) in
//possible features implemented later
UIApplication.shared.isStatusBarHidden = true
})
}
First you have to distinguish the buttons added in the cell by giving them tag value in tableView:cellForRowAtIndexPath: method
cell.button.tag = indexpath.row
This will set different tag values to the buttons present in each cell.
Then add below to it:
cell.button.addTarget(self, action:(YourController.buttonMethodPlayVideo(:)) , forControlEvents: .TouchUpInside)
Create a method for button to perform action on click:
func buttonMethodPlayVideo(sender: UIButton) {
print(sender.tag)
}
In above method when you will click on button you will get different tag values.
According to that value you can play different videos or pass different video name to player to play vudeo.
Setup an array to put your videos' paths into, and its indexe should match the index of the cell. In addition, you can pass the row number of the cell to the button inside as its tag. So when you tap on that button, you can find a specific video path in the array through the button's tag.

Where to put code if I want each row to display a different video file? [duplicate]

Currently I have my code setup to where in each table view cell there is a button that displays a video after is pressed. Each table view cell row contains the button (there are x amount of cells) however no matter which row the button is tapped in it always leads to the same video. Is there a way to make to where, depending on the row the button is in, it displays a video? My code only has one video file in it currently but how could I make it to where depending on the cell the button is tapped in, it shows a specific video? For example, if the button is tapped in row one I want it to show a certain video, and the same for two, and three, and so on. Right now they all display the same video.
Here is my code for the table view cell:
import UIKit
import AVFoundation
import AVKit
class VideoPlayerView: UIView {
let pauseButton: UIButton = {
let button = UIButton(type: .system)
button.setImage(#imageLiteral(resourceName: "Triangle 2"), for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.tintColor = UIColor.white
button.isHidden = false
button.addTarget(self, action: #selector(handlePause), for: .touchUpInside)
return button
}()
var player: AVPlayer?
var isPlaying = false
func handlePause() {
if isPlaying {
player?.pause()
pauseButton.alpha = 1.0 }
else { player?.play()
pauseButton.alpha = 0.01
}
isPlaying = !isPlaying
}
//container view that holds sublayers for the video control objects
let controlsContainerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(white: 0, alpha: 1.0)
return view
}()
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
//setupPlayerView()
//configures container view (video's background)
controlsContainerView.frame = frame
addSubview(controlsContainerView)
backgroundColor = UIColor.black
//following adds pause/play button to video
controlsContainerView.addSubview(pauseButton)
pauseButton.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
pauseButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
}
//function that sets up video playback
private func setupPlayerView() {
//variable that contains video url
let fileUrl = URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/dunk.mov")
player = AVPlayer(url: fileUrl)
//video only renders if you specify 'playerLayer'
let playerLayer = AVPlayerLayer(player: player)
self.layer.insertSublayer(playerLayer, at: 1)
playerLayer.frame = frame
player?.play()
//attached obeserver of 'player' to tell when 'player' is ready
player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)
}
//method called every time you add obserever to an object
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
//strring that lets AVPlayer know its ready
if keyPath == "currentItem.loadedTimeRanges" {
//configures container view while video is playing
controlsContainerView.backgroundColor = UIColor.clear
pauseButton.alpha = 0.05
isPlaying = true
}
}
}
class DrillsTableViewCell: UITableViewCell {
var videoURL:[URL] = [URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/dunk.mov"), URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/mk.MOV")]
var video = URL(fileURLWithPath: String())
#IBOutlet weak var logoImage: UIImageView!
#IBOutlet weak var drillTitle: UILabel!
#IBOutlet weak var playButton: UIButton!
#IBAction func watchButton(_ sender: Any) {
print(123)
//controls video background view
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.backgroundColor = UIColor.white
view.frame = CGRect(x: 0.0, y: 0.0, width: keyWindow.frame.width, height: keyWindow.frame.height)
let videoPlayerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.width * 9 / 16)
let videoPlayerView = VideoPlayerView(frame: videoPlayerFrame)
view.addSubview(videoPlayerView)
keyWindow.addSubview(view)
UIView.animate(
withDuration: 0.5,
delay: 0,
options: .curveEaseOut,
animations: {
view.frame = keyWindow.frame
},
completion: { completedAnimation in
//possible features implemented later
UIApplication.shared.isStatusBarHidden = true
}
)
}
}
}
Code for table view:
class DrillsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var arrayForKey2 = [[String]]()
var keyIndex = Int()
var headLabel = String()
var labels = Array(trainingDict.keys)
#IBOutlet weak var tableView: DrillsTableView!
#IBOutlet weak var drillLabel: UILabel!
#IBOutlet weak var labelBackground: UIView!
#IBAction func back(_ sender: Any) {
performSegue(withIdentifier: "back", sender: self)
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayForKey2.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell" , for: indexPath) as! DrillsTableViewCell
cell.playButton.tag = indexPath.row
//clear background color needed in order to display gradient cell
cell.backgroundColor = UIColor.clear
//gradient configuration
gradient = CAGradientLayer()
gradient.frame = tableView.bounds
gradient.colors = [UIColor.black.cgColor, UIColor.darkGray.cgColor, UIColor.black.cgColor]
tableView.layer.insertSublayer(gradient, at: 0)
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
//Possible method for 'drillLabel' gradient
drillLabel.font = UIFont(name: "Symbol", size: 24.0)
//attributes for watch/play button
cell.playButton.layer.shadowColor = UIColor.black.cgColor
cell.playButton.layer.shadowOffset = CGSize(width: 2, height: 2)
cell.playButton.layer.shadowOpacity = 0.7
cell.playButton.layer.shadowRadius = 1
//details for cell label display
cell.borderWidth = 1.5
cell.borderColor = UIColor.white
cell.drillTitle.text = "\(arrayForKey2[keyIndex][indexPath.row])"
cell.drillTitle.font = UIFont(name: "Symbol", size: 18.0)
cell.drillTitle.textColor = UIColor.white
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
drillLabel.text = labels[keyIndex]
}
}
I believe you should refactor your code to get required behaviour. Please check the following code:
First make changes in VideoPlayerView method named setupPlayerView .Replace your implementation with this:
func setupPlayerView(for url: URL) {
player = AVPlayer(url: url)
//video only renders if you specify 'playerLayer'
let playerLayer = AVPlayerLayer(player: player)
self.layer.insertSublayer(playerLayer, at: 1)
playerLayer.frame = frame
player?.play()
//attached obeserver of 'player' to tell when 'player' is ready
player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)
}
Now make change in DrillsTableViewCell ,make changes in videosURLs and I added a new variable singleVideoURL, your new class will look like this:
class DrillsTableViewCell: UITableViewCell {
var videoURLs:[URL] = [URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/dunk.mov"), URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BlackHeartBB/mk.MOV")]
#IBOutlet weak var logoImage: UIImageView!
#IBOutlet weak var drillTitle: UILabel!
#IBOutlet weak var playButton: UIButton!
#IBAction func watchButton(_ sender: UIButton) {
print(123)
//controls video background view
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.backgroundColor = UIColor.white
var singleVideoURL = videoURLs[sender.tag]
view.frame = CGRect(x: 0.0, y: 0.0, width: keyWindow.frame.width, height: keyWindow.frame.height)
let videoPlayerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.width * 9 / 16)
let videoPlayerView = VideoPlayerView(frame: videoPlayerFrame)
videoPlayerView .setupPlayerView(for: singleVideoURL)
view.addSubview(videoPlayerView)
keyWindow.addSubview(view)
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
view.frame = keyWindow.frame
}, completion: { (completedAnimation) in
//possible features implemented later
UIApplication.shared.isStatusBarHidden = true
})
}
First you have to distinguish the buttons added in the cell by giving them tag value in tableView:cellForRowAtIndexPath: method
cell.button.tag = indexpath.row
This will set different tag values to the buttons present in each cell.
Then add below to it:
cell.button.addTarget(self, action:(YourController.buttonMethodPlayVideo(:)) , forControlEvents: .TouchUpInside)
Create a method for button to perform action on click:
func buttonMethodPlayVideo(sender: UIButton) {
print(sender.tag)
}
In above method when you will click on button you will get different tag values.
According to that value you can play different videos or pass different video name to player to play vudeo.
Setup an array to put your videos' paths into, and its indexe should match the index of the cell. In addition, you can pass the row number of the cell to the button inside as its tag. So when you tap on that button, you can find a specific video path in the array through the button's tag.

Resources