- Fixed player issue
This commit is contained in:
@@ -54,8 +54,7 @@ class ThemeOneVC: UIViewController {
|
||||
NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
|
||||
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: K.NotificationCenterReloads.reloadTheme), object: nil)
|
||||
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: K.NotificationCenterReloads.themeOnePush), object: nil)
|
||||
NetworkMonitor.shared.stopMonitoring()
|
||||
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.connectivityStatus, object: nil)
|
||||
// NotificationCenter.default.removeObserver(self, name: NSNotification.Name.connectivityStatus, object: nil)
|
||||
if let playerItem = vm.playerItem{
|
||||
playerItem.removeObserver(self, forKeyPath: "status")
|
||||
}
|
||||
@@ -63,8 +62,8 @@ class ThemeOneVC: UIViewController {
|
||||
avPlayer.removeObserver(self, forKeyPath: "timeControlStatus")
|
||||
avPlayer.pause()
|
||||
}
|
||||
|
||||
NotificationCenter.default.removeObserver(self, name: AVAudioSession.routeChangeNotification, object: nil)
|
||||
// NetworkMonitor.shared.stopMonitoring()
|
||||
}
|
||||
|
||||
override var preferredStatusBarStyle: UIStatusBarStyle {
|
||||
@@ -79,13 +78,7 @@ class ThemeOneVC: UIViewController {
|
||||
if MyListDataTemp.shareInstance.favListingData?.showData == nil {
|
||||
MyListDataTemp.shareInstance.favListingData = FavouriteListingDM.ResultData(totalRecords: nil, showData: FavouriteListingDM.ResultData.ShowData(hindi: [],english: []),videoData: [],gameData: [],singKaraokeData: [],audioData: [])
|
||||
}
|
||||
// NetworkMonitor.shared.startMonitoring()
|
||||
// connectedToNetwork()
|
||||
|
||||
// PersistentStorage.shared.deleteData()
|
||||
// PersistentStorage.shared.retrieveData()
|
||||
vm.setupAvPlayer()
|
||||
|
||||
}
|
||||
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
@@ -160,22 +153,30 @@ class ThemeOneVC: UIViewController {
|
||||
// MARK: - Handle Live TV Observer
|
||||
|
||||
extension ThemeOneVC{
|
||||
func startMonitoring(){
|
||||
NetworkReachibility.shared.startMonitoring { [weak self] isConnected in
|
||||
|
||||
func startMonitoringNetwork() {
|
||||
if vm.isNetworkMonitored{return}
|
||||
vm.isNetworkMonitored = true
|
||||
vm.monitor.pathUpdateHandler = { [weak self] path in
|
||||
guard let self else{return}
|
||||
switch isConnected{
|
||||
case true: // if network is connected than stop
|
||||
if path.status == .satisfied {
|
||||
// when internet is back we have to setup player again
|
||||
NetworkReachibility.shared.stopMonitoring()
|
||||
vm.avPlayer = nil
|
||||
vm.setupAvPlayer()
|
||||
vm.monitor.cancel()
|
||||
DispatchQueue.main.async {
|
||||
self.vm.avPlayer = nil
|
||||
self.vm.setupAvPlayer()
|
||||
}
|
||||
print("network connection is back")
|
||||
case false:
|
||||
// if there is no internet connection prompt user and show reload icon
|
||||
self.toast(msg: "Lost Connection.", time: 1.4)
|
||||
// liveTVActivityIndicator.stopAnimating()
|
||||
} else {
|
||||
print("No connection.")
|
||||
|
||||
}
|
||||
print("Is on WiFi: \(path.usesInterfaceType(.wifi))")
|
||||
print("Is on Cellular: \(path.usesInterfaceType(.cellular))")
|
||||
}
|
||||
|
||||
vm.monitor.start(queue: vm.queue)
|
||||
vm.isNetworkMonitored = true
|
||||
}
|
||||
|
||||
func addObservers() {
|
||||
@@ -199,7 +200,12 @@ extension ThemeOneVC{
|
||||
vm.avPlayer.play()
|
||||
} else if vm.playerItem.status == .failed {
|
||||
print("Theme 1 Player failed to load")
|
||||
vm.setupAvPlayer()
|
||||
/*
|
||||
Check if network is connected.
|
||||
*/
|
||||
if vm.reachability?.isReachable == true{
|
||||
vm.setupAvPlayer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,6 +216,7 @@ extension ThemeOneVC{
|
||||
case .waitingToPlayAtSpecifiedRate:
|
||||
vm.startStopActivity(isStart: true)
|
||||
print("Theme 1 Player Buffering...")
|
||||
|
||||
switch vm.reachability?.isReachable{
|
||||
case true:
|
||||
break
|
||||
@@ -218,7 +225,10 @@ extension ThemeOneVC{
|
||||
Start monitoring when the player buffers and there is not internet connection
|
||||
*/
|
||||
print("Monitoring Started")
|
||||
NetworkMonitor.shared.startMonitoring()
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.4){ [weak self] in
|
||||
guard let self else{return}
|
||||
self.startMonitoringNetwork()
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
@@ -98,16 +98,43 @@ class ThemeTwoVC: UIViewController {
|
||||
|
||||
// MARK: - Handler for AvPlayer
|
||||
|
||||
func startMonitoringNetwork() {
|
||||
if vm.isNetworkMonitored{return}
|
||||
vm.isNetworkMonitored = true
|
||||
vm.monitor.pathUpdateHandler = { [weak self] path in
|
||||
guard let self else{return}
|
||||
if path.status == .satisfied {
|
||||
// when internet is back we have to setup player again
|
||||
vm.monitor.cancel()
|
||||
DispatchQueue.main.async {
|
||||
self.vm.avPlayer = nil
|
||||
self.vm.setupAvPlayer()
|
||||
}
|
||||
print("network connection is back")
|
||||
} else {
|
||||
print("No connection.")
|
||||
|
||||
}
|
||||
print("Is on WiFi: \(path.usesInterfaceType(.wifi))")
|
||||
print("Is on Cellular: \(path.usesInterfaceType(.cellular))")
|
||||
}
|
||||
|
||||
vm.monitor.start(queue: vm.queue)
|
||||
vm.isNetworkMonitored = true
|
||||
}
|
||||
|
||||
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
|
||||
|
||||
//this will oberver if player loads the url or fails
|
||||
if keyPath == "status" {
|
||||
if vm.playerItem.status == .readyToPlay {
|
||||
print("Theme 1 Player is ready to play")
|
||||
print("Theme 2 Player is ready to play")
|
||||
vm.avPlayer.play()
|
||||
} else if vm.playerItem.status == .failed {
|
||||
print("Theme 1 Player failed to load")
|
||||
vm.setupAvPlayer()
|
||||
print("Theme 2 Player failed to load")
|
||||
if vm.reachability?.isReachable == true{
|
||||
vm.setupAvPlayer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,13 +143,28 @@ class ThemeTwoVC: UIViewController {
|
||||
if let player = object as? AVPlayer {
|
||||
switch player.timeControlStatus {
|
||||
case .waitingToPlayAtSpecifiedRate:
|
||||
print("Theme 1 Player Buffering...")
|
||||
print("Theme 2 Player Buffering...")
|
||||
vm.startStopActivity(isStart: true)
|
||||
switch vm.reachability?.isReachable{
|
||||
case true:
|
||||
break
|
||||
case false:
|
||||
/*
|
||||
Start monitoring when the player buffers and there is not internet connection
|
||||
*/
|
||||
print("Monitoring Started")
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.4){ [weak self] in
|
||||
guard let self else{return}
|
||||
self.startMonitoringNetwork()
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
case .playing:
|
||||
print("Theme 1 Player Playing")
|
||||
print("Theme 2 Player Playing")
|
||||
vm.startStopActivity(isStart: false)
|
||||
case .paused:
|
||||
print("Theme 1 Player Paused")
|
||||
print("Theme 2 Player Paused")
|
||||
|
||||
// check if app is in background return it
|
||||
if UIApplication.shared.applicationState == .background {return}
|
||||
|
||||
@@ -23,7 +23,10 @@ class ThemeOneVM{
|
||||
|
||||
var shouldAnimate = true
|
||||
let reachability = NetworkReachabilityManager()
|
||||
|
||||
let monitor = NWPathMonitor()
|
||||
let queue = DispatchQueue.global(qos: .background)
|
||||
var isNetworkMonitored = false
|
||||
|
||||
func initView(){
|
||||
AuthFunc.shareInstance.initTimePeriods()
|
||||
startInitialTimer()
|
||||
@@ -56,28 +59,28 @@ class ThemeOneVM{
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(self.reloadTheme), name: NSNotification.Name(rawValue: K.NotificationCenterReloads.reloadTheme), object: nil)
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange(_:)), name: AVAudioSession.routeChangeNotification, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(showOfflineDeviceUI(notification:)), name: NSNotification.Name.connectivityStatus, object: nil)
|
||||
}
|
||||
|
||||
@objc func showOfflineDeviceUI(notification: Notification) {
|
||||
if NetworkMonitor.shared.isConnected {
|
||||
NetworkMonitor.shared.stopMonitoring()
|
||||
guard let avPlayer else{return}
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
guard let self else{return}
|
||||
if avPlayer.currentItem == nil || avPlayer.currentItem?.status == .failed {
|
||||
print("Player was Nil")
|
||||
setupAvPlayer()
|
||||
} else {
|
||||
print("Player is not Nil")
|
||||
setupAvPlayer()
|
||||
}
|
||||
}
|
||||
print("Connected")
|
||||
} else {
|
||||
print("Not connected")
|
||||
}
|
||||
// NotificationCenter.default.addObserver(self, selector: #selector(showOfflineDeviceUI(notification:)), name: NSNotification.Name.connectivityStatus, object: nil)
|
||||
}
|
||||
|
||||
// @objc func showOfflineDeviceUI(notification: Notification) {
|
||||
// if NetworkMonitor.shared.isConnected {
|
||||
// NetworkMonitor.shared.stopMonitoring()
|
||||
// guard let avPlayer else{return}
|
||||
// DispatchQueue.main.async { [weak self] in
|
||||
// guard let self else{return}
|
||||
// if avPlayer.currentItem == nil || avPlayer.currentItem?.status == .failed {
|
||||
// print("Player was Nil")
|
||||
// setupAvPlayer()
|
||||
// } else {
|
||||
// print("Player is not Nil")
|
||||
// setupAvPlayer()
|
||||
// }
|
||||
// }
|
||||
// print("Connected")
|
||||
// } else {
|
||||
// print("Not connected")
|
||||
// }
|
||||
// }
|
||||
|
||||
@objc func handleRouteChange(_ notification: Notification) {
|
||||
guard let userInfo = notification.userInfo,
|
||||
@@ -349,7 +352,7 @@ class ThemeOneVM{
|
||||
self.vc.liveTvPlayer.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
|
||||
|
||||
self.vc.liveTvPlayer.layer.addSublayer(playerLayer)
|
||||
|
||||
playerLayer.frame = vc.liveTvPlayer.bounds
|
||||
avPlayer.volume = 0
|
||||
vc.addObservers()
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
import UIKit
|
||||
import AVFoundation
|
||||
import JWPlayerKit
|
||||
import Alamofire
|
||||
|
||||
struct Theme2Struct{
|
||||
let imageName : String
|
||||
@@ -22,6 +23,11 @@ class ThemeTwoVM{
|
||||
var playerItem: AVPlayerItem!
|
||||
var playerLayer: AVPlayerLayer!
|
||||
|
||||
let reachability = NetworkReachabilityManager()
|
||||
let monitor = NWPathMonitor()
|
||||
let queue = DispatchQueue.global(qos: .background)
|
||||
var isNetworkMonitored = false
|
||||
|
||||
/*
|
||||
Static cell data
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user