In my iPhone application I want to keep play video when application enter in background mode.
I am using AVPlayer and not finding any way to play video in background
You can achive your requirement by using below sets of below code.
Swift: 4.2
Just, Create subclass of UIViewController
import UIKit
import AVFoundation
class VideoPlayer:UIViewController{
var avPlayer: AVPlayer!
var avPlayerLayer: AVPlayerLayer!
var paused: Bool = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
view.backgroundColor = .clear
avPlayer?.play()
paused = false
}
override func viewDidLoad() {
super.viewDidLoad()
let theURL = Bundle.main.url(forResource:"VIDEO_NAME", withExtension: "VIDEO_TYPE") //VIDEO_TYPE -> MP4
avPlayer = AVPlayer(url: theURL!)
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
avPlayer.volume = 0
avPlayer.actionAtItemEnd = .none
avPlayerLayer.frame = view.layer.bounds
view.backgroundColor = .clear
view.layer.insertSublayer(avPlayerLayer, at: 0)
addPlayerNotifications()
}
deinit {
removePlayerNotifations()
}
func addPlayerNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: avPlayer.currentItem)
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
}
func removePlayerNotifations() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
NotificationCenter.default.removeObserver(self, name:UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.removeObserver(self, name:UIApplication.didEnterBackgroundNotification, object: nil)
}
@objc func playerItemDidReachEnd(notification: Notification) {
let p: AVPlayerItem = notification.object as! AVPlayerItem
p.seek(to: CMTime.zero)
}
//App enter in forground.
@objc func applicationWillEnterForeground(_ notification: Notification) {
paused = false
avPlayer?.play()
}
//App enter in forground.
@objc func applicationDidEnterBackground(_ notification: Notification) {
paused = true
avPlayer?.pause()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
avPlayer.pause()
paused = true
}
}
Now, Almoast done. Just create UIViewcontroller class by using sub-class as below.
class Classname: VideoPlayer{
override func viewDidLoad() {
super.viewDidLoad()
}
}