Skip to content

Commit

Permalink
misc: replay live when video stream pause
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed May 13, 2024
1 parent 9baa7e8 commit 16cda66
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ class CommonPlayerViewController: AVPlayerViewController {
rateObserver = player.observe(\.rate, options: [.old, .new]) {
[weak self] player, _ in
guard let self = self else { return }
if player.rate > 0, self.danMuView.status == .pause {
self.danMuView.play()
} else if player.rate == 0, self.danMuView.status == .play {
self.danMuView.pause()
}
playerRateDidChange(player: player)
}
danMuView.play()
} else {
rateObserver = nil
}
Expand Down Expand Up @@ -117,6 +114,8 @@ class CommonPlayerViewController: AVPlayerViewController {
}
}

func playerRateDidChange(player: AVPlayer) {}

func setPlayerInfo(title: String?, subTitle: String?, desp: String?, pic: URL?) {
let desp = desp?.components(separatedBy: "\n").joined(separator: " ")
let mapping: [AVMetadataIdentifier: Any?] = [
Expand Down
9 changes: 8 additions & 1 deletion BilibiliLive/Component/Video/VideoPlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ class VideoPlayerViewController: CommonPlayerViewController {
let requestedKeys = ["playable"]
await asset.loadValues(forKeys: requestedKeys)
prepare(toPlay: asset, withKeys: requestedKeys)
danMuView.play()
updatePlayerCharpter(playerInfo: playerInfo)
BiliBiliUpnpDMR.shared.sendVideoSwitch(aid: playInfo.aid, cid: playInfo.cid ?? 0)
}
Expand Down Expand Up @@ -160,6 +159,14 @@ class VideoPlayerViewController: CommonPlayerViewController {
}
}

override func playerRateDidChange(player: AVPlayer) {
if player.rate > 0, danMuView.status == .pause {
danMuView.play()
} else if player.rate == 0, danMuView.status == .play {
danMuView.pause()
}
}

func playNext() -> Bool {
if let next = nextProvider?.getNext() {
playInfo = next
Expand Down
14 changes: 13 additions & 1 deletion BilibiliLive/Module/Live/LivePlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class LivePlayerViewController: CommonPlayerViewController {
setPlayerInfo(title: room?.title, subTitle: "nil", desp: room?.ownerName, pic: room?.pic)
}
}
danMuView.play()
}

override func viewWillDisappear(_ animated: Bool) {
Expand All @@ -72,6 +71,19 @@ class LivePlayerViewController: CommonPlayerViewController {
return true
}

override func playerRateDidChange(player: AVPlayer) {
Logger.info("play speed change to", player.rate)
if player.rate == 0 {
Task {
do {
try await initPlayer()
} catch let err {
endWithError(err: err)
}
}
}
}

func play() {
if let url = playInfo.first?.url {
danMuView.play()
Expand Down

0 comments on commit 16cda66

Please sign in to comment.