美文网首页
iOS后台写入剪切板

iOS后台写入剪切板

作者: 乱尘 | 来源:发表于2023-03-30 14:34 被阅读0次

    只需要开启画中画就可以后台写入了

    
    import UIKit
    import AVKit
    
    class HomeVC: BaseViewController {
        var playerLayer: AVPlayerLayer?
        var pipPlayer: AVPlayer?
        var pipController: AVPictureInPictureController?
        
    }
    
    extension HomeVC: AVPictureInPictureControllerDelegate {
        func createPlayer() {
            // 加载视频
            guard let path = Bundle.main.path(forResource: "TEST", ofType: "mp4") else { return }
            // 加载视频
            let videoURL = URL(fileURLWithPath: path)
            let asset = AVAsset(url: videoURL)
            let playerItem = AVPlayerItem(asset: asset)
            pipPlayer = AVPlayer(playerItem: playerItem)
            
            // 创建视频播放器
            let playerLayer = AVPlayerLayer(player: pipPlayer)
            playerLayer.frame = CGRectMake(0, 0, screen_width * 0.4, screen_height * 0.4)
            self.playerLayer = playerLayer
            let videoPlayerView = UIView(frame: CGRectMake(0, screen_height * 0.3, screen_width * 0.4, screen_height * 0.4))
            videoPlayerView.layer.addSublayer(playerLayer)
            view.addSubview(videoPlayerView)
            videoPlayerView.isHidden = true
            
            if AVPictureInPictureController.isPictureInPictureSupported() {
                let session: AVAudioSession = AVAudioSession.sharedInstance()
                do {
                    try session.setCategory(.playback, mode: .moviePlayback, options: [.interruptSpokenAudioAndMixWithOthers])
                    try session.setActive(true)
                } catch  {
                    print(error.localizedDescription)
                }
                pipController = AVPictureInPictureController(playerLayer: playerLayer)
                pipController?.delegate = self
            }
        }
        
        // 开始画中画
        func startPictureInPicture() {
            if AVPictureInPictureController.isPictureInPictureSupported() {
                pipController?.startPictureInPicture()
            }
        }
        
        // 开始画中画
        func endPictureInPicture() {
            pipController?.stopPictureInPicture()
        }
    
        
        // 实现画中画控制器的代理方法
        func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
            // 在开始画中画时,将视频暂停并隐藏播放器视图
            pipPlayer?.seek(to: .zero)
            pipPlayer?.play()
        }
        
        func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
            // 在结束画中画时,显示播放器视图并继续播放视频
            pipPlayer?.pause()
            scrollContent.slider_c?.isOn = false
            Defaults[\.SWAutoCopy] = false
        }
    }
    
    

    相关文章

      网友评论

          本文标题:iOS后台写入剪切板

          本文链接:https://www.haomeiwen.com/subject/gqqcddtx.html