-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVPlayer.swift
57 lines (46 loc) · 1.46 KB
/
AVPlayer.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
//
// AVPlayer.swift
// Created by Umair on 07/06/2020.
// Copyright © 2020 Umair . All rights reserved.
import UIKit
import AVKit
import AVFoundation
class VideoView: UIView {
var playerLayer: AVPlayerLayer?
var player: AVPlayer?
var isLoop: Bool = false
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func configure(url: String) {
if let videoURL = URL(string: url) {
player = AVPlayer(url: videoURL)
playerLayer = AVPlayerLayer(player: player)
playerLayer?.frame = bounds
playerLayer?.videoGravity = AVLayerVideoGravity.resize
if let playerLayer = self.playerLayer {
layer.addSublayer(playerLayer)
}
NotificationCenter.default.addObserver(self, selector: #selector(reachTheEndOfTheVideo(_:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
}
}
func play() {
if player?.timeControlStatus != AVPlayer.TimeControlStatus.playing {
player?.play()
}
}
func pause() {
player?.pause()
}
func stop() {
player?.pause()
player?.seek(to: CMTime.zero)
}
@objc func reachTheEndOfTheVideo(_ notification: Notification) {
if isLoop {
player?.pause()
player?.seek(to: CMTime.zero)
player?.play()
}
}
}