Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持设置默认播放速度并且在播放开始时显示当前播放速度 #130

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion BilibiliLive/Component/Player/Plugins/SpeedChangerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,29 @@
import AVKit

class SpeedChangerPlugin: NSObject, CommonPlayerPlugin {
private var notifyView: UILabel?
private weak var containerView: UIView?
private weak var player: AVPlayer?
private weak var playerVC: AVPlayerViewController?

@Published private(set) var currentPlaySpeed: PlaySpeed = .default

func addViewToPlayerOverlay(container: UIView) {
containerView = container
}

private func fadeOutNotifyView() {
UIView.animate(withDuration: 1.0, animations: {
self.notifyView?.alpha = 0.0 // Fade out to invisible
}) { _ in
self.notifyView?.removeFromSuperview() // Optionally remove from superview after fading out
}
}

func playerDidLoad(playerVC: AVPlayerViewController) {
self.playerVC = playerVC

currentPlaySpeed = Settings.mediaPlayerSpeed
}

func playerDidChange(player: AVPlayer) {
Expand All @@ -25,6 +41,32 @@ class SpeedChangerPlugin: NSObject, CommonPlayerPlugin {
playerVC?.selectSpeed(AVPlaybackSpeed(rate: currentPlaySpeed.value, localizedName: currentPlaySpeed.name))
}

func playerDidStart(player: AVPlayer) {
if notifyView == nil {
notifyView = UILabel()
notifyView?.backgroundColor = UIColor.black.withAlphaComponent(0.3)
notifyView?.textColor = UIColor.white
containerView?.addSubview(notifyView!)
notifyView?.numberOfLines = 0
notifyView?.layer.cornerRadius = 10 // Set the corner radius
notifyView?.layer.masksToBounds = true // Enable masks to bounds
notifyView?.font = UIFont.systemFont(ofSize: 26)
notifyView?.textAlignment = NSTextAlignment.center
notifyView?.snp.makeConstraints { make in
// make.bottom.equalToSuperview().inset(20) // 20 points from the bottom
make.center.equalToSuperview() // Center horizontally
make.width.equalTo(300) // Set a width (optional)
make.height.equalTo(60) // Set a height (optional)
}
}
notifyView?.isHidden = false
notifyView?.text = "播放速度设置为 \(currentPlaySpeed.name)"

DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.fadeOutNotifyView()
}
}

func addMenuItems(current: inout [UIMenuElement]) -> [UIMenuElement] {
let gearImage = UIImage(systemName: "gearshape")

Expand All @@ -43,7 +85,7 @@ class SpeedChangerPlugin: NSObject, CommonPlayerPlugin {
}
}

struct PlaySpeed {
struct PlaySpeed: Codable {
var name: String
var value: Float
}
Expand Down
3 changes: 3 additions & 0 deletions BilibiliLive/Component/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ enum Settings {
@UserDefaultCodable("Settings.mediaQuality", defaultValue: .quality_1080p)
static var mediaQuality: MediaQualityEnum

@UserDefaultCodable("Settings.mediaPlayerSpeed", defaultValue: PlaySpeed.default)
static var mediaPlayerSpeed: PlaySpeed

@UserDefaultCodable("Settings.danmuArea", defaultValue: .style_75)
static var danmuArea: DanmuArea

Expand Down
6 changes: 6 additions & 0 deletions BilibiliLive/Module/Personal/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ class SettingsViewController: UIViewController {
optionString: MediaQualityEnum.allCases.map({ $0.desp })) {
Settings.mediaQuality = $0
}
Actions(title: "默认播放速度", message: "默认设置为1.0",
current: Settings.mediaPlayerSpeed.name,
options: PlaySpeed.blDefaults,
optionString: PlaySpeed.blDefaults.map({ $0.name })) {
Settings.mediaPlayerSpeed = $0
}
Toggle(title: "Avc优先(卡顿尝试开启)", setting: Settings.preferAvc, onChange: Settings.preferAvc.toggle())
Toggle(title: "无损音频和杜比全景声", setting: Settings.losslessAudio, onChange: Settings.losslessAudio.toggle())
Toggle(title: "匹配视频内容", setting: Settings.contentMatch, onChange: Settings.contentMatch.toggle())
Expand Down
Loading