Commit 989cda52 authored by Alex朱枝文's avatar Alex朱枝文

优化直播间逻辑

parent 404928a3
......@@ -8,7 +8,7 @@
import UIKit
import FSPagerView
import JXPageControl
//import JXPageControl
class YHAIChatBannerItem {
var id: Int = 0
......
......@@ -37,16 +37,10 @@ class YHBasePlayerViewController: YHBaseViewController {
return view
}()
// 控制状态
private var isControlsVisible = true
private var controlsAutoHideTimer: Timer?
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
//setupGestures()
//setupNotifications()
}
override func viewWillAppear(_ animated: Bool) {
......@@ -62,8 +56,6 @@ class YHBasePlayerViewController: YHBaseViewController {
gk_navBarAlpha = 1
gk_navigationBar.isHidden = false
view.backgroundColor = .black
// controlsAutoHideTimer?.invalidate()
// controlsAutoHideTimer = nil
UIApplication.shared.isIdleTimerDisabled = false
}
......@@ -96,111 +88,6 @@ class YHBasePlayerViewController: YHBaseViewController {
make.height.equalTo(k_Height_NavigationtBarAndStatuBar)
}
}
// private func setupGestures() {
// let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
// containerView.addGestureRecognizer(tap)
// }
//
// // MARK: - Controls Visibility
// @objc private func handleTap() {
// toggleControls()
// }
//
// private func toggleControls() {
// isControlsVisible.toggle()
// //controlView.showControls(isControlsVisible)
// resetControlsAutoHideTimer()
// }
//
// private func resetControlsAutoHideTimer() {
// controlsAutoHideTimer?.invalidate()
// if isControlsVisible {
// controlsAutoHideTimer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false) { [weak self] _ in
// self?.hideControls()
// }
// }
// }
//
// private func hideControls() {
// isControlsVisible = false
// //controlView.showControls(false)
// }
}
// MARK: - Notifications
extension YHBasePlayerViewController {
private func setupNotifications() {
NotificationCenter.default.addObserver(
self,
selector: #selector(handleAppDidEnterBackground),
name: UIApplication.didEnterBackgroundNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleAppWillEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleAudioSessionInterruption),
name: AVAudioSession.interruptionNotification,
object: nil
)
}
@objc private func handleAppDidEnterBackground() {
YHPlayerManager.shared.pause()
}
@objc private func handleAppWillEnterForeground() {
YHPlayerManager.shared.resume()
}
@objc private func handleAudioSessionInterruption(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
let type = AVAudioSession.InterruptionType(rawValue: typeValue) else {
return
}
switch type {
case .began:
YHPlayerManager.shared.pause()
case .ended:
if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
if options.contains(.shouldResume) {
YHPlayerManager.shared.resume()
}
}
@unknown default:
break
}
}
}
// MARK: - Helper Methods
extension YHBasePlayerViewController {
func formatTime(_ timeInMilliseconds: Int) -> String {
let totalSeconds = timeInMilliseconds / 1000
let minutes = totalSeconds / 60
let seconds = totalSeconds % 60
return String(format: "%02d:%02d", minutes, seconds)
}
func showAlert(message: String) {
let alert = UIAlertController(title: "提示",
message: message,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "确定", style: .default))
present(alert, animated: true)
}
}
// MARK: - Orientation Support
......
......@@ -8,6 +8,7 @@
import AgoraRtcKit
import HyphenateChat
//import AVKit
import UIKit
class YHLivePlayerViewController: YHBasePlayerViewController {
......@@ -59,7 +60,7 @@ class YHLivePlayerViewController: YHBasePlayerViewController {
}
vc.closeEvent = { [weak self] in
self?.leaveLiveRoom()
//self?.leaveLiveRoom()
self?.closeLive()
}
......@@ -87,12 +88,6 @@ class YHLivePlayerViewController: YHBasePlayerViewController {
return imageView
}()
// private lazy var blurredView: YHBlurredAvatarView = {
// let view = YHBlurredAvatarView()
// view.isHidden = true
// return view
// }()
// MARK: - Initialization
init(id: Int, url: String? = nil, title: String? = nil, roomId: String? = nil) {
......@@ -123,6 +118,7 @@ class YHLivePlayerViewController: YHBasePlayerViewController {
joinLiveRoom(id: liveId, callback: { _, _ in })
}
setupTimer()
setupLifeCycleNotifications()
}
override func viewWillAppear(_ animated: Bool) {
......@@ -695,7 +691,8 @@ extension YHLivePlayerViewController: YHPlayerDelegate {
// 直播开始时的特殊处理
break
case .failed:
self.showAlert(message: "播放失败,错误原因:\(reason.rawValue)")
break
//self.showAlert(message: "播放失败,错误原因:\(reason.rawValue)")
default:
break
}
......@@ -717,3 +714,67 @@ extension YHLivePlayerViewController: YHPlayerDelegate {
#endif
}
}
// MARK: - Notifications
extension YHLivePlayerViewController {
private func setupLifeCycleNotifications() {
NotificationCenter.default.addObserver(
self,
selector: #selector(handleAppDidEnterBackground),
name: UIApplication.didEnterBackgroundNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleAppWillEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil
)
/*
NotificationCenter.default.addObserver(
self,
selector: #selector(handleAudioSessionInterruption),
name: AVAudioSession.interruptionNotification,
object: nil
)
*/
}
@objc private func handleAppDidEnterBackground() {
leaveLiveRoom()
}
@objc private func handleAppWillEnterForeground() {
if YHLoginManager.shared.isLogin() {
joinLiveRoom(id: liveId, callback: { _, _ in })
}
}
/*
@objc private func handleAudioSessionInterruption(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
let type = AVAudioSession.InterruptionType(rawValue: typeValue) else {
return
}
switch type {
case .began:
if let player = player {
YHPlayerManager.shared.leaveChannel(for: player)
}
case .ended:
if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
if options.contains(.shouldResume) {
if let liveDetail = viewModel.liveDetailModel, !liveDetail.rtmp_channel.isEmpty, !liveDetail.token.isEmpty, let uid = playbackInfo?.uid, let player = player {
YHPlayerManager.shared.joinChannel(for: player, token: liveDetail.token, channelId: liveDetail.rtmp_channel, uid: uid, view: playerView, defaultMuted: false)
}
}
}
@unknown default:
break
}
}
*/
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment