I'm sure this is a rather simple and common problem - however I am new to programming and cannot find an answer to this online. I'm trying to create code that plays a video on loop in the background of an app. I've created a UIView
and put the video into it. Now I'm trying to put the code behind it and I'm running into issues.
I have two error messages:
'seek(to:)' was deprecated in iOS 11.0: Use -seekToTime:completionHandler:, passing nil for the completionHandler if you don't require notification of completion
and, after trying to run the app
Thread 1: signal SIGTERM
I've tried just about everything including rewriting the entire section of code (I had actually got it running before, even with the first error listed above), to no avail.
I have heard around the web that the SIGTERM issue can be fixed by restarting, but this is not the case and I think the issue is a result of the first error which I will explain next.
Here's my code:
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
@IBOutlet weak var videoView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
private func setupView() {
let path = URL(fileURLWithPath: Bundle.main.path(forResource: "Cafe Video", ofType: "mov")!)
let player = AVPlayer(url: path)
let newLayer = AVPlayerLayer(player: player)
newLayer.frame = self.videoView.frame
self.videoView.layer.addSublayer(newLayer)
newLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
player.play()
player.actionAtItemEnd = AVPlayer.ActionAtItemEnd.none
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.videoDidPlayToEnd(_:)), name: NSNotification.Name(rawValue: "AVPlayerItemDidPlayToEndTimeNotification"), object: player.currentItem)
}
@objc func videoDidPlayToEnd(_ notification: Notification) {
let player: AVPlayerItem = notification.object as! AVPlayerItem
player.seek(to: CMTime.zero)
}
}
The first error message is associated with the very last line of code (player.seek(to: CMTime.zero).
I would like to be able to run my program and have my video play and loop. Instead, I get a blank white screen on the simulator, an error on my player.seek line and this SIGTERM thing in the AppDelegate after trying to run the app.
Any help that can be provided re this will be much appreciated - even if it does not entirely solve my problem per se.
Update following code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setupView()
}
In setupView() function
let newLayer = AVPlayerLayer(player: player)
newLayer.frame = self.videoView.bounds
Update Observer
@objc func videoDidPlayToEnd(_ notification: Notification) {
let player: AVPlayerItem = notification.object as! AVPlayerItem
player.seek(to: CMTime.zero, completionHandler: nil)
}