- fixed bugs for screen orientation for jwplayer

- fixed bugs for screen orientation for games
This commit is contained in:
Bilal
2024-08-01 00:08:56 +05:30
parent 4c69628a94
commit 9ecf0ad050
4 changed files with 145 additions and 174 deletions

View File

@@ -19,6 +19,9 @@ class GamesWebViewVC: UIViewController {
super.viewDidLoad()
if let orientation, orientation == .landscape{
appDelegate.deviceOrientation = .landscapeRight
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
rotateToLandsScapeDevice()
}
@@ -32,29 +35,29 @@ class GamesWebViewVC: UIViewController {
}
func rotateToLandsScapeDevice(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.myOrientation = .landscapeRight
if #available(iOS 16.0, *) {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: UIInterfaceOrientationMask.landscapeRight))
} else {
UIDevice.current.setValue(UIInterfaceOrientationMask.landscapeRight.rawValue, forKey: "orientation")
}
UIView.setAnimationsEnabled(true)
// let appDelegate = UIApplication.shared.delegate as! AppDelegate
// appDelegate.myOrientation = .landscapeRight
// if #available(iOS 16.0, *) {
// let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
// windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: UIInterfaceOrientationMask.landscapeRight))
// } else {
// UIDevice.current.setValue(UIInterfaceOrientationMask.landscapeRight.rawValue, forKey: "orientation")
// }
// UIView.setAnimationsEnabled(true)
}
func rotateToPotraitScapeDevice(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.myOrientation = .portrait
if #available(iOS 16.0, *) {
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: UIInterfaceOrientationMask.portrait))
} else {
UIDevice.current.setValue(UIInterfaceOrientationMask.landscapeRight.rawValue, forKey: "orientation")
}
// UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UIView.setAnimationsEnabled(true)
// let appDelegate = UIApplication.shared.delegate as! AppDelegate
// appDelegate.myOrientation = .portrait
// if #available(iOS 16.0, *) {
// let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
// windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: UIInterfaceOrientationMask.portrait))
// } else {
// UIDevice.current.setValue(UIInterfaceOrientationMask.landscapeRight.rawValue, forKey: "orientation")
// }
//// UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
// UIView.setAnimationsEnabled(true)
}
@IBAction func backBtnTapped(_ sender: UIButton) {
@@ -67,10 +70,14 @@ class GamesWebViewVC: UIViewController {
vcPush.onDoneBlock = { mode in
switch mode{
case .yes:
self.rotateToPotraitScapeDevice()
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
self.dismiss(animated: true)
}
appDelegate.deviceOrientation = .portrait
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
// Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
// self.dismiss(animated: true)
// }
case .no:
print("no")
}
@@ -80,4 +87,25 @@ class GamesWebViewVC: UIViewController {
self.present(vcPush, animated: true)
}
// MARK: - Handle Screen Transition
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: nil) { _ in
self.checkOrientation()
}
}
private func checkOrientation() {
let isPortrait = UIScreen.main.bounds.size.width < UIScreen.main.bounds.size.height
if isPortrait {
print("Device is in portrait mode")
Timer.scheduledTimer(withTimeInterval: 0.4, repeats: false) { _ in
self.dismiss(animated: true)
}
} else {
print("Device is in landscape mode")
}
}
}

View File

@@ -11,15 +11,24 @@ import IQKeyboardManagerSwift
import JWPlayerKit
import Firebase
let appDelegate = UIApplication.shared.delegate as! AppDelegate
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
// var window: UIWindow?
//
// var myOrientation: UIInterfaceOrientationMask = .portrait
//
// func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
// return myOrientation
// }
var window: UIWindow?
var myOrientation: UIInterfaceOrientationMask = .portrait
var deviceOrientation = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return myOrientation
return deviceOrientation
}
private func registerCustomFonts() {

View File

@@ -995,7 +995,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="dli-ld-4dd">
<rect key="frame" x="40" y="79" width="30" height="30"/>
<rect key="frame" x="20" y="79" width="30" height="30"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="HPJ-GV-z0x"/>
@@ -1018,7 +1018,7 @@
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="dli-ld-4dd" firstAttribute="top" secondItem="2gp-j2-aWC" secondAttribute="top" constant="20" id="WZP-PS-eKd"/>
<constraint firstItem="dli-ld-4dd" firstAttribute="leading" secondItem="2gp-j2-aWC" secondAttribute="leading" constant="40" id="jRV-6s-U6h"/>
<constraint firstItem="dli-ld-4dd" firstAttribute="leading" secondItem="2gp-j2-aWC" secondAttribute="leading" constant="20" id="jRV-6s-U6h"/>
</constraints>
</view>
<connections>
@@ -1249,10 +1249,10 @@
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
<systemColor name="systemBrownColor">
<color red="0.63529411764705879" green="0.51764705882352946" blue="0.36862745098039218" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color red="0.63529411759999999" green="0.51764705879999995" blue="0.36862745099999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
<systemColor name="systemGreenColor">
<color red="0.20392156862745098" green="0.7803921568627451" blue="0.34901960784313724" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color red="0.20392156859999999" green="0.78039215689999997" blue="0.34901960780000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
</resources>
</document>

View File

@@ -19,73 +19,43 @@ class PlayerVC: JWPlayerViewController, JWPlayerViewControllerDelegate {
var config: JWPlayerConfiguration!
var dismissTapped: (() -> Void)?
var videoIndex : Int?
override var prefersStatusBarHidden: Bool {
return true
}
var isFullScreenBtn = false
// func rotateToLandsScapeDevice(){
// let appDelegate = UIApplication.shared.delegate as! AppDelegate
// appDelegate.myOrientation = .landscapeRight
// if #available(iOS 16.0, *) {
//// let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
// appDelegate.window?.windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: UIInterfaceOrientationMask.landscapeRight), errorHandler: { error in
// print(error)
// })
// } else {
// UIDevice.current.setValue(UIInterfaceOrientationMask.landscapeRight.rawValue, forKey: "orientation")
// }
// UIView.setAnimationsEnabled(true)
// }
//
// func rotateToPortraitScapeDevice(){
// let appDelegate = UIApplication.shared.delegate as! AppDelegate
// appDelegate.myOrientation = .portrait
// if #available(iOS 16.0, *) {
// let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
// windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: UIInterfaceOrientationMask.portrait), errorHandler: { error in
// print(error)
// })
// } else {
// UIDevice.current.setValue(UIInterfaceOrientationMask.portrait.rawValue, forKey: "orientation")
// }
// UIView.setAnimationsEnabled(true)
// }
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: nil) { _ in
let isPortrait = size.width < size.height
if isPortrait {
print("Device is in portrait mode")
} else {
print("Device is in landscape mode")
}
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .allButUpsideDown
}
override var shouldAutorotate: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
// self.rotateToLandsScapeDevice()
rotateView(to: .pi / 2) // Example: 90 degrees rotation
// rotateView(to: .pi / 2) // Example: 90 degrees rotation
//bring back button to the front
self.view.bringSubviewToFront(backButton)
}
func rotateView(to angle: CGFloat) {
// Apply rotation to the view's transform
view.transform = CGAffineTransform(rotationAngle: angle)
}
// func rotateView(to angle: CGFloat) {
// // Apply rotation to the view's transform
// view.transform = CGAffineTransform(rotationAngle: angle)
// }
@objc func applicationDidBecomeActive() {
// self.setDeviceOrientation(orientation: .landscapeRight)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
appDelegate.deviceOrientation = .landscapeRight
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
player.configurePlayer(with: config)
self.delegate = self
//Disable Picture in Picture
playerView.allowsPictureInPicturePlayback = false
playerView.captionStyle = .none
@@ -97,15 +67,49 @@ class PlayerVC: JWPlayerViewController, JWPlayerViewControllerDelegate {
}
@IBAction func backBtnTapped(_ sender: UIButton) {
// self.transitionToFullScreen(animated: true)
handleBackAction()
}
func handleBackAction(){
self.interfaceBehavior = .hidden
self.player.stop()
if contentType == .liveStream{
self.dismissTapped?()
}
self.dismiss(animated: true)
appDelegate.deviceOrientation = .portrait
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
}
// MARK: - Handle Screen Transition
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: nil) { _ in
self.checkOrientation()
}
}
private func checkOrientation() {
let isPortrait = UIScreen.main.bounds.size.width < UIScreen.main.bounds.size.height
if isPortrait {
print("Device is in portrait mode")
self.dismiss(animated: true)
} else {
print("Device is in landscape mode")
if isFullScreenBtn{
appDelegate.deviceOrientation = .portrait
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
}
}
}
// MARK: - JWPlayerViewControllerDelegate
override func jwplayer(_ player: any JWPlayer, didFinishLoadingWithTime loadTime: TimeInterval) {
@@ -132,7 +136,6 @@ class PlayerVC: JWPlayerViewController, JWPlayerViewControllerDelegate {
}
case .trailer,.songs:
break
// player.play()
case .continueWatching,.audioBooks, .games:
player.seek(to: 0)
player.play()
@@ -152,8 +155,19 @@ class PlayerVC: JWPlayerViewController, JWPlayerViewControllerDelegate {
super.jwplayer(player, failedWithError: code, message: message)
print("Error: \(code) - \(message)")
self.player.configurePlayer(with: config)
self.player.play()
Utilities.alertWithBtnCancelCompletion(title: "Error", msgBody: message, okBtnStr: "Connect", vc: self) { [weak self] isDone in
guard let self else{
self?.handleBackAction()
return
}
if isDone{
self.player.configurePlayer(with: config)
self.player.play()
}else{
self.handleBackAction()
}
}
}
override func jwplayer(_ player: JWPlayer, encounteredWarning code: UInt, message: String) {
@@ -175,7 +189,6 @@ class PlayerVC: JWPlayerViewController, JWPlayerViewControllerDelegate {
override func jwplayer(_ player: JWPlayer, isBufferingWithReason reason: JWBufferReason) {
super.jwplayer(player, isBufferingWithReason: reason)
// player.play()
print("Buffering Reason:", reason)
}
@@ -191,82 +204,34 @@ extension PlayerVC {
func playerViewControllerWillGoFullScreen(_ controller: JWPlayerViewController) -> JWFullScreenViewController? {
print("playerViewControllerWillGoFullScreen")
// controller.player.stop()
// self.player.stop()
// controller.dismissFullScreen(animated: true)
// self.rotateToPortraitScapeDevice()
self.interfaceBehavior = .hidden
self.player.stop()
controller.player.stop()
controller.dismissFullScreen(animated: true)
if contentType == .liveStream{
self.dismissTapped?()
}
self.dismiss(animated: true)
// self.player.stop()
// Timer.scheduledTimer(withTimeInterval: 0.8, repeats: false) { _ in
// DispatchQueue.main.async { [weak self] in
// guard let self else{return}
// if contentType == .liveStream{
// self.dismissTapped?()
// }
// self.dismiss(animated: true)
// }
// }
return nil
}
func playerViewControllerDidGoFullScreen(_ controller: JWPlayerViewController) {
print("playerViewControllerDidGoFullScreen")
// self.interfaceBehavior = .hidden
// self.player.stop()
// controller.player.stop()
// controller.dismissFullScreen(animated: true)
//// self.player.stop()
// Timer.scheduledTimer(withTimeInterval: 0.8, repeats: false) { _ in
// DispatchQueue.main.async { [weak self] in
// guard let self else{return}
// if contentType == .liveStream{
// self.dismissTapped?()
// }
// self.dismiss(animated: true)
// }
// }
self.player.pause()
self.dismissFullScreen(animated: false)
self.dismiss(animated: true)
return
}
func playerViewControllerWillDismissFullScreen(_ controller: JWPlayerViewController) {
print("playerViewControllerWillDismissFullScreen")
// self.setDeviceOrientation(orientation: .portrait)
// Timer.scheduledTimer(withTimeInterval: 0.4, repeats: false) { _ in
// controller.dismissFullScreen(animated: false) {
// self.dismissTapped?()
// }
// }
// self.rotateToPotraitScapeDevice()
// self.dismiss
// self.setDeviceOrientation(orientation: .portrait)
// Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
// controller.dismiss(animated: false) {
// }
// self.setDeviceOrientation(orientation: .portrait)
// }
}
func playerViewControllerDidDismissFullScreen(_ controller: JWPlayerViewController) {
print("playerViewControllerDidDismissFullScreen")
// self.dismissTapped?()
// Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { _ in
// self.player.stop()
// controller.dismiss(animated: true)
// }
// self.navigationController?.popViewController(animated: true)
appDelegate.deviceOrientation = .portrait
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
}
}
@@ -298,34 +263,3 @@ extension PlayerVC {
print("Item ", item)
}
}
// MARK: - Orientation Handling
//
//extension UIViewController {
//
// func setDeviceOrientation(orientation: UIInterfaceOrientationMask) {
// if #available(iOS 16.0, *) {
// let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
// windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation))
// } else {
// UIDevice.current.setValue(orientation.toUIInterfaceOrientation.rawValue, forKey: "orientation")
// }
// }
//}
//
//extension UIInterfaceOrientationMask {
// var toUIInterfaceOrientation: UIInterfaceOrientation {
// switch self {
// case .portrait:
// return .portrait
// case .portraitUpsideDown:
// return .portraitUpsideDown
// case .landscapeRight:
// return .landscapeRight
// case .landscapeLeft:
// return .landscapeLeft
// default:
// return .unknown
// }
// }
//}