Fork me on GitHub

iOS tutorial 12:视频背景

  • 新建项目 BackgroundVideoDemo
  • 新建BackgroundVideo.swift文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    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中定义:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
        override func viewDidLoad() {
    super.viewDidLoad()

    let backgroundPlayer = BackgroundVideo(on: self, withVideoURL: "test.mp4")
    //是否打开静音
    // backgroundPlayer?.isMuted = false

    backgroundPlayer.setUpBackground()

    }

详细代码: BackgroundVideoDemo

参考:BackgroundVideoiOS


--------------------本文结束👨‍💻感谢您的阅读--------------------
坚持原创技术分享,您的支持将鼓励我继续创作!
  • 本文标题: iOS tutorial 12:视频背景
  • 本文作者: AndyRon
  • 发布时间: 2017年08月20日 - 00:00
  • 最后更新: 2019年09月03日 - 13:46
  • 本文链接: http://andyron.com/2017/ios-tutorial-12.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!
0%