I’m trying to record UIView which has several playing AVPlayerLayer…!
Howevver it doesn’t work when I record that UIView.
It shows just Red Screen(Because I set red color to that view)
-> Videos are not shown…!
import UIKit
import AVFoundation
class ViewController: UIViewController {
var screenRecorder: ScreenRecorder?
var playerView: PlayerView?
var player: AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
// PlayerView 설정
playerView = PlayerView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
if let playerView = playerView {
self.view.addSubview(playerView)
setupPlayer(in: playerView)
}
// ScreenRecorder 초기화
if let playerView = playerView {
screenRecorder = ScreenRecorder(view: playerView)
}
// 녹화 버튼 추가
let startButton = UIButton(frame: CGRect(x: 20, y: 400, width: 100, height: 50))
startButton.setTitle("Start", for: .normal)
startButton.backgroundColor = .blue
startButton.addTarget(self, action: #selector(startRecording), for: .touchUpInside)
self.view.addSubview(startButton)
let stopButton = UIButton(frame: CGRect(x: 150, y: 400, width: 100, height: 50))
stopButton.setTitle("Stop", for: .normal)
stopButton.backgroundColor = .red
stopButton.addTarget(self, action: #selector(stopRecording), for: .touchUpInside)
self.view.addSubview(stopButton)
}
private func setupPlayer(in view: PlayerView) {
guard let path = Bundle.main.path(forResource: "video", ofType: "mp4") else { return }
let url = URL(fileURLWithPath: path)
player = AVPlayer(url: url)
playerView?.playerLayer?.player = player
player?.play()
}
@objc func startRecording() {
screenRecorder?.startRecording()
}
@objc func stopRecording() {
player?.pause() // 비디오 재생을 멈춤
screenRecorder?.stopRecording { [weak self] outputURL in
guard let url = outputURL else { return }
print("Video saved to: (url)")
self?.navigateToSharingViewController(with: url)
}
}
private func navigateToSharingViewController(with url: URL) {
let sharingViewController = SharingViewController()
sharingViewController.videoURL = url
navigationController?.pushViewController(sharingViewController, animated: true)
}
}
-> This is my VC to show one AVplayerlayer…! But it also doesn’t work
New contributor
Xerath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.