Avplayer showing black screen while playing on iOS 17.3.1 with low probability

Some users of our app get a black screen when playing mp4 files on their phones. The users are all running iOS 17.3.1 and cannot catch the error message when playing a black screen.

The user can play the video normally after restarting the mobile phone. However, we want to solve this problem from the code.

We use two Avplayers to complete the sequential playback of multiple videos.

    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.addSublayer(videoPlayerA)
        layer.addSublayer(videoPlayerB)
    }

    lazy var layers = [videoPlayerA, videoPlayerB]

    lazy var videoPlayerA: AVPlayerLayer = {
        let avPlayerLayer = AVPlayerLayer(player: AVPlayer())
        avPlayerLayer.contentsScale = UIScreen.main.scale
        avPlayerLayer.videoGravity = .resizeAspectFill
        return avPlayerLayer
    }()

    lazy var videoPlayerB: AVPlayerLayer = {
        let avPlayerLayer = AVPlayerLayer(player: AVPlayer())
        avPlayerLayer.contentsScale = UIScreen.main.scale
        avPlayerLayer.videoGravity = .resizeAspectFill
        return avPlayerLayer
    }()

    func play() {
        if let path = paths[safe: currentIndex] {
            currentVideoPath = path
            currentPlayer = AVPlayer(playerItem: AVPlayerItem(url: URL(fileURLWithPath: path)))
            layers.first?.player?.pause()
            layer.insertSublayer(layers.first ?? AVPlayerLayer(), below: layers.last)
            layers.swapAt(0, 1)
            layers.first?.player = currentPlayer
            layers.first?.player?.play()
            SHPlayerGlobalStatus.sharedInstance.isLocalPlayerPlaying = true
            try? AVAudioSession.sharedInstance().setCategory(.playback)
            try? AVAudioSession.sharedInstance().setActive(true)
        } 

Avplayer showing black screen while playing on iOS 17.3.1 with low probability
 
 
Q