Implement multiple video views in UIView iOS swift
Problem:
I want to implement two video's in my view with play and pause options.Also show play button.
While scroll my scrollview i want pause the video.
How to achieve the both above?
Solution:
import AVFoundation
import RxSwift
import RxCocoa
class MyVC: UIViewController {
@IBOutlet weak var videoView: UIView!
@IBOutlet weak var videoPlayBtn: UIButton!
@IBOutlet weak var videoView1: UIView!
@IBOutlet weak var videoPlayBtn1: UIButton!
var player: AVPlayer!
var playerLayer = AVPlayerLayer()
var isPlayer: Bool! = false
var player1: AVPlayer!
var playerLayer1 = AVPlayerLayer()
var isPlayer1: Bool! = false
// MARK: - viewDidLoad
override func viewDidLoad() {
try! AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: [])
self.videoPlayBtn.tintColor = .lightGray
self.videoPlayBtn1.tintColor = .lightGray
}
override func viewDidDisappear(_ animated: Bool) {
self.player.removeObserver(self, forKeyPath: "rate")
self.player1.removeObserver(self, forKeyPath: "rate")
}
// MARK: - setupbinding
func setupBinding() {
videoPlayer()
videoPlayer1()
self.videoPlayBtn.rx.tap.asDriver()
.drive(onNext: { [weak self] in
self?.isPlayer = !(self?. isPlayer)!
if self?.player != nil {
self?.isPlayer == true ? self?. player.play() : self?. player.pause()
}
}).disposed(by: self.disposebag)
videoView.rx.tapGesture()
.when(.recognized) // This is important!
.subscribe(onNext: { [weak self] _ in
guard let self = self else { return }
self.isPlayer = !(self.isPlayer)!
if self.player != nil {
self.isPlayer == true ? self.player.play() : self.player.pause()
}
self.view?.endEditing(true)
}).disposed(by: self.disposebag)
self.videoPlayBtn.setTitle("", for: .normal)
self.videoPlayBtn.setImage(UIImage(named: "play_icon"), for: .normal)
self.videoPlayBtn1.rx.tap.asDriver()
.drive(onNext: { [weak self] in
self?.isPlayer1 = !(self?.isPlayer1)!
if self?.player1 != nil {
self?.isPlayer1 == true ? self?.player1.play() : self?.player1.pause()
}
}).disposed(by: self.disposebag)
videoView1.rx.tapGesture()
.when(.recognized) // This is important!
.subscribe(onNext: { [weak self] _ in
guard let self = self else { return }
self.isPlayer1 = !(self.isPlayer1)!
if self.player1 != nil {
self.isPlayer1 == true ? self.player1.play() : self.player1.pause()
}
self.view?.endEditing(true)
}).disposed(by: self.disposebag)
self.videoPlayBtn1.setTitle("", for: .normal)
self.videoPlayBtn1.setImage(UIImage(named: "play_icon"), for: .normal)
}
override func viewDidLayoutSubviews() {
playerLayer.frame = CGRectMake(0, 0, videoView.bounds.width, videoView.bounds.height)
playerLayer.masksToBounds = true
playerLayer.videoGravity = .resizeAspect
playerLayer1.frame = CGRectMake(0, 0, smartVideoView.bounds.width, videoView1.bounds.height)
playerLayer1.masksToBounds = true
playerLayer1.videoGravity = .resizeAspect
}
func videoPlayer() {
guard let path = Bundle.main.path(forResource: "myVIdeo", ofType:"mp4") else {
printMsg("video.m4v not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRectMake(0, 0, videoView.bounds.width, videoView.bounds.height)
self.videoView.layer.addSublayer(playerLayer)
playerLayer.masksToBounds = true
playerLayer.videoGravity = .resizeAspectFill
self.videoView.bringSubviewToFront(videoPlayBtn)
self.player.addObserver(self, forKeyPath: "rate", options: .new, context: nil)
}
func videoPlayer1() {
guard let path = Bundle.main.path(forResource: "myVIdeo1", ofType:"mp4") else {
printMsg("video.m4v not found")
return
}
player1 = AVPlayer(url: URL(fileURLWithPath: path))
playerLayer1 = AVPlayerLayer(player: player1)
playerLayer1.frame = CGRectMake(0, 0, VideoView1.bounds.width, VideoView1.bounds.height)
self.VideoView1.layer.addSublayer(playerLayer1)
playerLayer1.masksToBounds = true
playerLayer1.videoGravity = .resizeAspectFill
self.VideoView1.bringSubviewToFront(VideoPlayBtn1)
self.player1.addObserver(self, forKeyPath: "rate", options: .new, context: nil)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if player != nil {
player.pause()
}
if player1 != nil {
player1.pause()
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "rate" {
if (change?[NSKeyValueChangeKey.newKey] as? Float) == 0 {
self.videoPlayBtn.setImage(UIImage(named: "play_icon"), for: .normal)
} else if (change?[NSKeyValueChangeKey.newKey] as? Float) == 1 {
self.videoPlayBtn.setImage(nil, for: .normal)
if #available(iOS 15.0, *) {
self.videoPlayBtn.configuration?.image = nil
} else {
// Fallback on earlier versions
}
}
self.videoPlayBtn.clipsToBounds = true
self.videoPlayBtn.setNeedsDisplay()
self.videoPlayBtn.tintColor = .lightGray
}
if keyPath == "rate" {
if (change?[NSKeyValueChangeKey.newKey] as? Float) == 0 {
self.videoPlayBtn1.setImage(UIImage(named:"play_icon"), for: .normal)
self.VideoImage1.isHidden = false
} else if (change?[NSKeyValueChangeKey.newKey] as? Float) == 1 {
self.VideoImage1.isHidden = true
self.videoPlayBtn1.setImage(nil, for: .normal)
if #available(iOS 15.0, *) {
self.videoPlayBtn1.configuration?.image = nil
} else {
// Fallback on earlier versions
}
}
self.videoPlayBtn1.clipsToBounds = true
self.videoPlayBtn1.setNeedsDisplay()
self.videoPlayBtn1.tintColor = .lightGray
}
}
}
Comments
Post a Comment