ashish0309/AutoVideoPlayer

first video in a list is not playing

Opened this issue · 7 comments

Hi any one help first video in a tableview is not playing only sound is coming please provide me update on this issue

Hello @sudarvizhi3026 , Did you get any solution?

@ashish0309 Can you give a solution for Video in the First cell is not playing when it appears the first time, we have to drag its bottom slightly to play the video.

Plz, help me with that. Thanks in advance.

I got one solution for that, Put pausePlayeVideos() method in Api call after reload table view. This works for me.

I solved it with this. There is an issue with your current and fully visible item of collectionview.

Add this method in ASVideoPlayerController above pausePlayeVideosFor.

func fullyVisibleCells(_ inCollectionView: UICollectionView) -> [UICollectionViewCell] {

        var returnCells = [UICollectionViewCell]()

        var vCells = inCollectionView.visibleCells
        vCells = vCells.filter({ cell -> Bool in
            let cellRect = inCollectionView.convert(cell.frame, to: inCollectionView.superview)
            return inCollectionView.frame.contains(cellRect)
        })
        vCells.forEach({
            returnCells.append($0)
        })
        return returnCells
    }

Replace pausePlayeVideosFor to this

       func pausePlayeVideosFor(collectionView: UICollectionView, appEnteredFromBackground: Bool = false) {
        let visisbleCells = fullyVisibleCells(collectionView)
        var videoCellContainer: ASAutoPlayVideoLayerContainer?
        var maxHeight: CGFloat = 0.0
       
        for cellView in collectionView.visibleCells {
            guard let containerCell = cellView as? ASAutoPlayVideoLayerContainer,
                let videoCellURL = containerCell.videoURL else {
                    continue
            }
            let height = containerCell.visibleVideoHeight()
            if maxHeight < height {
                maxHeight = height
                videoCellContainer = containerCell
            }
            pauseRemoveLayer(layer: containerCell.videoLayer, url: videoCellURL, layerHeight: height)
        }
        
        guard let videoCell = visisbleCells.first as? ASAutoPlayVideoLayerContainer,
            let videoCellURL = videoCell.videoURL else {
                return
        }
        
        
        let minCellLayerHeight = videoCell.videoLayer.bounds.size.height * 0.5
        /**
         Visible video layer height should be at least more than max of predefined minimum height and
         cell's videolayer's 50% height to play video.
         */
        let minimumVideoLayerVisibleHeight = max(minCellLayerHeight, minimumLayerHeightToPlay)
        if maxHeight > minimumVideoLayerVisibleHeight {
            if appEnteredFromBackground {
                setupVideoFor(url: videoCellURL)
            }
            playVideo(withLayer: videoCell.videoLayer, url: videoCellURL)
        }
    }
 

I solved it with this. There is an issue with your current and fully visible item of collectionview.

Add this method in ASVideoPlayerController above pausePlayeVideosFor.

func fullyVisibleCells(_ inCollectionView: UICollectionView) -> [UICollectionViewCell] {

        var returnCells = [UICollectionViewCell]()

        var vCells = inCollectionView.visibleCells
        vCells = vCells.filter({ cell -> Bool in
            let cellRect = inCollectionView.convert(cell.frame, to: inCollectionView.superview)
            return inCollectionView.frame.contains(cellRect)
        })
        vCells.forEach({
            returnCells.append($0)
        })
        return returnCells
    }

Replace pausePlayeVideosFor to this

       func pausePlayeVideosFor(collectionView: UICollectionView, appEnteredFromBackground: Bool = false) {
        let visisbleCells = fullyVisibleCells(collectionView)
        var videoCellContainer: ASAutoPlayVideoLayerContainer?
        var maxHeight: CGFloat = 0.0
       
        for cellView in collectionView.visibleCells {
            guard let containerCell = cellView as? ASAutoPlayVideoLayerContainer,
                let videoCellURL = containerCell.videoURL else {
                    continue
            }
            let height = containerCell.visibleVideoHeight()
            if maxHeight < height {
                maxHeight = height
                videoCellContainer = containerCell
            }
            pauseRemoveLayer(layer: containerCell.videoLayer, url: videoCellURL, layerHeight: height)
        }
        
        guard let videoCell = visisbleCells.first as? ASAutoPlayVideoLayerContainer,
            let videoCellURL = videoCell.videoURL else {
                return
        }
        
        
        let minCellLayerHeight = videoCell.videoLayer.bounds.size.height * 0.5
        /**
         Visible video layer height should be at least more than max of predefined minimum height and
         cell's videolayer's 50% height to play video.
         */
        let minimumVideoLayerVisibleHeight = max(minCellLayerHeight, minimumLayerHeightToPlay)
        if maxHeight > minimumVideoLayerVisibleHeight {
            if appEnteredFromBackground {
                setupVideoFor(url: videoCellURL)
            }
            playVideo(withLayer: videoCell.videoLayer, url: videoCellURL)
        }
    }
 

Can You help to achieve my collection view with auto video support

try

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
                                    self.pausePlayeVideos()
                                }

after reloading the tableview / collectionview