Skip to content

Commit 7ba39bd

Browse files
authored
Merge pull request #2 from valentcheung/master
Added ability to set different videos for light + dark mode
2 parents a796afb + 0555b29 commit 7ba39bd

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

SDLoopingVideoView/SDLoopingVideoView.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import UIKit
1010
import AVKit
1111

1212
/// A looping video-view based off of AVPlayerLayer. Automatically scales video to aspect-fill, but can be manually set otherwise. Responds to UIView animations and scales accordingly.
13+
@available(iOS 12.0, *)
1314
public class SDLoopingVideoView: UIView {
1415

1516
enum VideoPropertiesNotSetError: Error {
1617
case runtimeError(String)
1718
}
1819

19-
@IBInspectable private var videoName: String?
20+
@IBInspectable private var video_Light: String?
21+
@IBInspectable private var video_Night: String?
2022
@IBInspectable private var videoType: String?
2123
private var player: AVPlayer?
2224
private var playerLayer: AVPlayerLayer { get { return (self.layer as! AVPlayerLayer) } }
@@ -34,26 +36,36 @@ public class SDLoopingVideoView: UIView {
3436

3537
- Returns: An SDLoopingVideoView
3638
*/
37-
public init(frame: CGRect, videoName: String, videoType: String, scaling: AVLayerVideoGravity = .resizeAspectFill) {
38-
self.videoName = videoName
39+
public init(frame: CGRect, videoName_Light: String, videoName_Night: String?, videoType: String, scaling: AVLayerVideoGravity = .resizeAspectFill) {
40+
self.video_Light = videoName_Light
41+
self.video_Night = videoName_Night
3942
self.videoType = videoType
4043
self.scaling = scaling
4144
super.init(frame:frame)
4245
attemptVideoSetup()
4346
}
4447

4548
private func setupVideoView() throws {
46-
guard videoName != nil && videoType != nil else {
49+
guard video_Light != nil && video_Night != nil && videoType != nil else {
4750
throw VideoPropertiesNotSetError.runtimeError("Video name or video type not set")
4851
}
4952
if player == nil {
5053
self.backgroundColor = UIColor.clear
5154
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
52-
guard let path = Bundle.main.path(forResource: self?.videoName!, ofType:self?.videoType!) else { debugPrint("video file not found")
53-
return }
54-
self?.player = AVPlayer(url: URL(fileURLWithPath: path))
55-
self?.player?.isMuted = true
55+
5656
DispatchQueue.main.async {
57+
var video: String {
58+
switch self?.traitCollection.userInterfaceStyle {
59+
case .light, .unspecified, .none:
60+
return self!.video_Light!
61+
case .dark:
62+
return self!.video_Night!
63+
}
64+
}
65+
guard let path = Bundle.main.path(forResource: video, ofType:self?.videoType!) else { debugPrint("video file not found")
66+
return }
67+
self?.player = AVPlayer(url: URL(fileURLWithPath: path))
68+
self?.player?.isMuted = true
5769
self?.playerLayer.player = self?.player
5870
self?.playerLayer.videoGravity = self?.scaling ?? AVLayerVideoGravity.resizeAspectFill
5971
self?.player!.play()
@@ -88,4 +100,5 @@ public class SDLoopingVideoView: UIView {
88100
super.layoutSubviews()
89101
attemptVideoSetup()
90102
}
103+
91104
}

0 commit comments

Comments
 (0)