Getting blank screen in avVideo player while entering to foreground in uitableviewCell

131 Views Asked by At

I am using AVPlayer for a custom video player and getting a blank screen while I enter from the background.

func playSingleVideo(pauseAll: Bool = false,foreground:Bool = false) {
    if let visibleCells = self.tableView.visibleCells as? [VideoCell], !visibleCells.isEmpty {
        if pauseAll {
            visibleCells.forEach { $0.playerLayer?.player?.pause() }
        } else {
            var maxHeightRequired: Int = 50
            var cellToPlay: VideoCell?
            
            visibleCells.reversed().forEach { (cell) in
                let cellBounds = self.view.convert(cell.videoView.frame, from: cell.videoView)
                let visibleCellHeight = Int(self.view.frame.intersection(cellBounds).height)
                
                if visibleCellHeight >= maxHeightRequired {
                    maxHeightRequired = visibleCellHeight
                    cellToPlay = cell
                }
            }
            
            visibleCells.forEach { (cell) in
                
                
                if cell === cellToPlay {
                    
                    cell.slider.minimumValue = 0.0

                    cell.playerLayer?.player?.play()
                    cell.btnPlay.setImage(UIImage(named: "pause-button") , for: .normal)
                    cell.btnPlay.setTitle("", for: .normal)

// cell.videoView.layer.insertSublayer(cell.playerLayer!, at: 0)

                } else {
                    cell.playerLayer?.player?.pause()
                    cell.btnPlay.setTitle("", for: .normal)

// cell.videoView.layer.insertSublayer(cell.playerLayer!, at: 0) cell.slider.minimumValue = 0.0 cell.btnPlay.setImage(UIImage(named: "play-button") , for: .normal)

                }
                NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: cell.playerLayer?.player?.currentItem, queue: .main) { [weak self] _ in
                    cell.playerLayer?.player?.seek(to: kCMTimeZero)
                    cell.playerLayer?.player?.play()
                }
                
                let interval = CMTime(value: 1, timescale: 2)
                cell.playerLayer?.player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: { (progressTime) in
                    
                    let seconds = CMTimeGetSeconds(progressTime)
                    let secondString = String(format: "%02d", Int(seconds) % 60)
                    let minutString = String(format: "%02d", Int(seconds) / 60)
                    print( "\(minutString):\(secondString)")
                    cell.lblElapsed.text = "\(minutString):\(secondString)"
                    cell.playerLayer?.player?.currentItem?.preferredForwardBufferDuration = TimeInterval(exactly: 100)!
                    cell.videoView.layer.insertSublayer(cell.playerLayer!, at: 0)
                    
                    guard let duration = cell.playerLayer?.player?.currentItem?.duration else { return  }
                    
                    
                    let seconds2 = CMTimeGetSeconds(duration)
                    if !seconds2.isNaN{
                        let sec = String(format: "%02d", Int(seconds2)  % 60)
                        let min = String(format: "%02d", Int(seconds2)  / 60)
                        print("\(min):\(sec)")
                        cell.lblTotal.text = "\(min):\(sec)"
                        let totsec = seconds / seconds2
                        
                        if !self.isended{
                            cell.slider.setValue(Float(totsec), animated: true)
                            
                        }
                    }
                })
            }
        }
    }
}
0

There are 0 best solutions below