Search code examples
iosswiftcapacitor

Capacitor Custom AVPlayer View Controller


I'm new to Swift / iOS, so my apologies if I may come across as daft. I am trying to play a custom video splashscreen for my capacitor iOS app on launch, but I seem to be having troubles switching view controllers using segues. On my Main storyboard, I have two View Controllers, the Capacitor Bridge View (which runs my react app perfectly fine), and I have an AVPlayer View Controller.

I have set my AVPlayer as the Initial View Controller and created a segue to the capacitor bridge view with the segue having id "test", and I call a performsegue() method in the custom AVPlayer controller at the end of the video, which doesn't do anything, despite it running.

Here is the custom class for my AVPlayer

//
//  AVPlayer.swift
//  App
//
//

import Foundation
import UIKit
import AVFoundation
import AVKit
import Capacitor


class viewControl: AVPlayerViewController {
    
    override func viewDidAppear(_ animated: Bool) {
            playVideo()
        }
        
        let playerController = AVPlayerViewController()
        private func playVideo() {
            guard let path = Bundle.main.path(forResource: "Splash_Screen_Christmas", ofType:"mp4") else {
                debugPrint("splash.m4v not found")
                return
            }
            let player = AVPlayer(url: URL(fileURLWithPath: path))
            playerController.showsPlaybackControls = false
            playerController.player = player
            playerController.videoGravity = .resizeAspectFill
            NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerController.player?.currentItem)
            present(playerController, animated: true) {
                player.play()
            }
        }
        @objc func playerDidFinishPlaying(note: NSNotification) {
           // let storyboard = UIStoryboard(name: "Main", bundle: nil)
            // let vc = storyboard.instantiateViewController(withIdentifier: "MainVC")
            performSegue(withIdentifier: "test", sender: self)
            print("Method, video is finished ")
        }
    
    
}

After the video plays, the simulator logs "video is finished" but the Bridge View Controller is not being pushed.


Solution

  • I resolved this issue by avoiding using a separate view controllers, for anyone using Capacitor to add a new view controller, you will need to switch view controllers by creating a subclass of the CAPBridgeViewController

    import UIKit
    import Capacitor
    import AVKit
    // Instantiate a subclass of CAPBridgeViewController
    
    class capController: CAPBridgeViewController {
        // Define video URL here
        let player: AVPlayer = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "Splash_Screen_Normal", ofType:"mp4")!))
        
          var playerLayer: AVPlayerLayer!
    
          override func viewWillAppear(_ animated: Bool) {
              super.viewWillAppear(animated)
    
              playerLayer = AVPlayerLayer(player: player)
              playerLayer.videoGravity = .resizeAspectFill
              playerLayer.frame = self.view.bounds
              view.layer.addSublayer(playerLayer)
              // Try catch to avoid breaking user background audio.
              do {
                  try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: .mixWithOthers)
                      try AVAudioSession.sharedInstance().setActive(true)
                 } catch {
                      print(error)
                 }
              // End of background audio code validation
              player.play()
              NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
          }
    
          @objc func playerDidFinishPlaying(note: NSNotification) {
              print("Video has finished running.")
              playerLayer.removeFromSuperlayer()
          }
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
        
    
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destination.
            // Pass the selected object to the new view controller.
        }
        */
    
    }