Skip to content

Commit

Permalink
Merge pull request #12 from mbernson/feature/prevent-sleep
Browse files Browse the repository at this point in the history
Prevent device from sleeping while playing video
  • Loading branch information
mbernson authored Jan 24, 2025
2 parents 53e95fd + 3af9eac commit a7bb305
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions CCCTube/Features/VideoPlayer/VideoPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import SwiftUI
struct VideoPlayerView: UIViewControllerRepresentable {
let player: AVPlayer?

func makeUIViewController(context: Context) -> AVPlayerViewController {
let playerViewController = AVPlayerViewController()
func makeUIViewController(context: Context) -> VideoPlayerViewController {
let playerViewController = VideoPlayerViewController()
playerViewController.delegate = context.coordinator
playerViewController.modalPresentationStyle = .fullScreen

Expand All @@ -29,15 +29,15 @@ struct VideoPlayerView: UIViewControllerRepresentable {
return playerViewController
}

func updateUIViewController(_ playerViewController: AVPlayerViewController, context: Context) {
func updateUIViewController(_ playerViewController: VideoPlayerViewController, context: Context) {
playerViewController.player = player
}

func makeCoordinator() -> VideoPlayerCoordinator {
VideoPlayerCoordinator()
}

static func dismantleUIViewController(_ playerViewController: AVPlayerViewController, coordinator: Coordinator) {
static func dismantleUIViewController(_ playerViewController: VideoPlayerViewController, coordinator: Coordinator) {
playerViewController.player?.cancelPendingPrerolls()
playerViewController.player?.pause()
}
Expand All @@ -50,3 +50,17 @@ class VideoPlayerCoordinator: NSObject, AVPlayerViewControllerDelegate {
logger.error("Failed to start picture in picture: \(error)")
}
}

class VideoPlayerViewController: AVPlayerViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

UIApplication.shared.isIdleTimerDisabled = true
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

UIApplication.shared.isIdleTimerDisabled = false
}
}

0 comments on commit a7bb305

Please sign in to comment.