Skip to content

Commit

Permalink
feat: 支持弹幕屏蔽等级&展示时长
Browse files Browse the repository at this point in the history
  • Loading branch information
hydewww committed Jul 15, 2023
1 parent 2b3424d commit f1f6359
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
15 changes: 12 additions & 3 deletions BilibiliLive/Component/Player/CommonPlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ class CommonPlayerViewController: AVPlayerViewController {
}
menus.append(danmuAction)

let danmuDurationMenu = UIMenu(title: "弹幕展示时长", options: [.displayInline, .singleSelection], children: [4, 6, 8].map { dur in
UIAction(title: "\(dur)", state: dur == Settings.danmuDuration ? .on : .off) { _ in Settings.danmuDuration = dur }
})
let danmuAILevelMenu = UIMenu(title: "弹幕屏蔽等级", options: [.displayInline, .singleSelection], children: [Int32](1 ... 10).map { level in
UIAction(title: "\(level)", state: level == Settings.danmuAILevel ? .on : .off) { _ in Settings.danmuAILevel = level }
})
let danmuSettingMenu = UIMenu(title: "弹幕设置", image: UIImage(systemName: "keyboard.badge.ellipsis"), children: [danmuDurationMenu, danmuAILevelMenu])
menus.append(danmuSettingMenu)

let debugEnableImage = UIImage(systemName: "terminal.fill")
let debugDisableImage = UIImage(systemName: "terminal")
let debugAction = UIAction(title: "Debug", image: debugEnable ? debugEnableImage : debugDisableImage) {
Expand Down Expand Up @@ -203,7 +212,7 @@ class CommonPlayerViewController: AVPlayerViewController {
PlaySpeed(name: "2X", value: 2)]

let speedActions = playSpeedArray.map { playSpeed in
UIAction(title: playSpeed.name, state: player?.rate ?? 1 == playSpeed.value ? .on : .off) { [weak self] action in
UIAction(title: playSpeed.name, state: player?.rate ?? 1 == playSpeed.value ? .on : .off) { [weak self] _ in
self?.player?.currentItem?.audioTimePitchAlgorithm = .timeDomain
if #available(tvOS 16.0, *) {
self?.selectSpeed(AVPlaybackSpeed(rate: playSpeed.value, localizedName: playSpeed.name))
Expand Down Expand Up @@ -286,7 +295,7 @@ class CommonPlayerViewController: AVPlayerViewController {
private func fetchDebugInfo() -> String {
let bitrateStr: (Double) -> String = {
bit in
return String(format: "%.2fMbps", bit / 1024.0 / 1024.0)
String(format: "%.2fMbps", bit / 1024.0 / 1024.0)
}

guard let log = player?.currentItem?.accessLog() else { return "no log" }
Expand Down Expand Up @@ -375,7 +384,7 @@ extension CommonPlayerViewController: AVPlayerViewControllerDelegate {
return false
}

@objc func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool {
@objc func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_: AVPlayerViewController) -> Bool {
return true
}

Expand Down
6 changes: 6 additions & 0 deletions BilibiliLive/Component/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ enum Settings {
@UserDefaultCodable("Settings.danmuSize", defaultValue: .size_36)
static var danmuSize: DanmuSize

@UserDefaultCodable("Settings.danmuAILevel", defaultValue: 1)
static var danmuAILevel: Int32

@UserDefaultCodable("Settings.danmuDuration", defaultValue: 8)
static var danmuDuration: Double

@UserDefault("Settings.losslessAudio", defaultValue: false)
static var losslessAudio: Bool

Expand Down
3 changes: 3 additions & 0 deletions BilibiliLive/Component/Video/VideoDanmuProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ struct Danmu: Codable {
var fontSize: Int32 = 25
var color: UInt32 = 16_777_215
var isUp: Bool = false
var aiLevel: Int32 = 0

init(dm: DanmakuElem) {
text = dm.content
time = TimeInterval(dm.progress / 1000)
mode = dm.mode
fontSize = dm.fontsize
color = dm.color
aiLevel = dm.weight
}

init(upDm dm: CommandDm) {
Expand Down Expand Up @@ -165,6 +167,7 @@ class VideoDanmuProvider {
let dm = dms[danmuIdx]
guard dm.time < time else { break }
danmuIdx += 1
if dm.aiLevel < Settings.danmuAILevel { continue }
onShowDanmu?(DanmakuTextCellModel(dm: dm))
}
}
Expand Down
2 changes: 1 addition & 1 deletion BilibiliLive/Vendor/DanmakuKit/DanmakuTextCellModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DanmakuTextCellModel: DanmakuCellModel, Equatable {

var track: UInt?

var displayTime: Double = 8
var displayTime: Double = Settings.danmuDuration

var type: DanmakuCellType = .floating

Expand Down

0 comments on commit f1f6359

Please sign in to comment.