- 新建项目 BackgroundVideoDemo
- 新建
BackgroundVideo.swift
文件:
import Foundation
import AVKit
import AVFoundation
enum BackgroundVideoErrors: Error {
case invalidVideo
}
class BackgroundVideo {
var isMuted = true
private var player : AVPlayer?
private var videoURL: URL?
private var viewController: UIViewController?
private var hasBeenUsed: Bool = false
init (on viewController: UIViewController, withVideoURL URL: String) {
self.viewController = viewController
// parse the video string to split it into name and extension
let videoNameAndExtension:[String]? = URL.characters.split{$0 == "."}.map(String.init)
if videoNameAndExtension!.count == 2 {
if let videoName = videoNameAndExtension?[0] , let videoExtension = videoNameAndExtension?[1] {
if let url = Bundle.main.url(forResource: videoName, withExtension: videoExtension) {
self.videoURL = url
// initialize our player with our fetched video url
self.player = AVPlayer(url: self.videoURL!)
} else {
print(BackgroundVideoErrors.invalidVideo)
}
}
} else {
print("Wrong video name format")
}
}
deinit{
if self.hasBeenUsed {
NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIApplicationWillEnterForeground, object: nil)
}
}
public func setUpBackground(){
self.player?.actionAtItemEnd = .none
self.player?.isMuted = isMuted
//add the video to your view ..
let loginView: UIView = self.viewController!.view//get our view controllers view
let playerLayer = AVPlayerLayer(player: self.player)
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill // preserve aspect ratio and resize to fill screen
playerLayer.zPosition = -1 // set it's possition behined anything in our view
playerLayer.frame = loginView.frame // set our player frame to our view's frame
loginView.layer.addSublayer(playerLayer)
// prevent video from disturbing audio services from other apps
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
}
catch {
print("failed setting AVAudioSession Category to AVAudioSessionCategoryAmbient")
}
self.player?.play() // start the video
/// 向通知中心发送视频结束后再次从头播放
NotificationCenter.default.addObserver(self, selector: #selector(self.loopVideo), name: .AVPlayerItemDidPlayToEndTime, object: nil)
// 视频进入Foreground后从头播放
NotificationCenter.default.addObserver(self, selector: #selector(self.loopVideo), name: .UIApplicationWillEnterForeground, object: nil)
self.hasBeenUsed = true
}
// 循环播放视频
@objc private func loopVideo() {
self.player?.seek(to: kCMTimeZero)
self.player?.play()
}
}
- 在所需要的视图控制器中定义变量
var backgroundPlayer : BackgroundVideo?
,然后再在viewDidLoad
中定义:
override func viewDidLoad() {
super.viewDidLoad()
let backgroundPlayer = BackgroundVideo(on: self, withVideoURL: "test.mp4")
//是否打开静音
// backgroundPlayer?.isMuted = false
backgroundPlayer.setUpBackground()
}
详细代码: BackgroundVideoDemo
网友评论