Skip to content

Commit

Permalink
fix #528
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Nov 11, 2023
1 parent c2d69ed commit 16472af
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Sources/KSPlayer/AVPlayer/MediaPlayerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public protocol MediaPlayback: AnyObject {
func seek(time: TimeInterval, completion: @escaping ((Bool) -> Void))
}

public class DynamicInfo {
public class DynamicInfo: ObservableObject {
private let metadataBlock: () -> [String: String]
private let bytesReadBlock: () -> Int64
private let audioBitrateBlock: () -> Int
Expand Down
21 changes: 21 additions & 0 deletions Sources/KSPlayer/AVPlayer/PlayerDefines.swift
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,27 @@ public extension Int {
}
}

public extension FixedWidthInteger {
var kmFormatted: String {
Double(self).kmFormatted
}
}

public extension Double {
var kmFormatted: String {
// return .formatted(.number.notation(.compactName))
if self >= 1_000_000 {
return String(format: "%.1fM", locale: Locale.current, self / 1_000_000)
// .replacingOccurrences(of: ".0", with: "")
} else if self >= 10000, self <= 999_999 {
return String(format: "%.1fK", locale: Locale.current, self / 1000)
// .replacingOccurrences(of: ".0", with: "")
} else {
return String(format: "%.0f", locale: Locale.current, self)
}
}
}

extension TextAlignment: RawRepresentable {
public typealias RawValue = String
public init?(rawValue: RawValue) {
Expand Down
46 changes: 29 additions & 17 deletions Sources/KSPlayer/SwiftUI/KSVideoPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ extension View {
}
}

@available(iOS 15, tvOS 16, macOS 12, *)
@available(iOS 16, tvOS 16, macOS 13, *)
struct VideoControllerView: View {
@ObservedObject
fileprivate var config: KSVideoPlayer.Coordinator
Expand Down Expand Up @@ -374,7 +374,8 @@ extension EventModifiers {

@available(iOS 16, tvOS 16, macOS 13, *)
struct VideoSubtitleView: View {
@ObservedObject fileprivate var model: SubtitleModel
@ObservedObject
fileprivate var model: SubtitleModel
var body: some View {
ZStack {
ForEach(model.parts) { part in
Expand Down Expand Up @@ -440,7 +441,7 @@ fileprivate extension SubtitlePart {
}
}

@available(iOS 15, tvOS 16, macOS 12, *)
@available(iOS 16, tvOS 16, macOS 13, *)
struct VideoSettingView: View {
@ObservedObject
fileprivate var config: KSVideoPlayer.Coordinator
Expand Down Expand Up @@ -522,31 +523,39 @@ struct VideoSettingView: View {
Button("Search Sutitle") {
subtitleModel.searchSubtitle(query: subtitleTitle, languages: ["zh-cn"])
}
Text("Stream Type: \((videoTracks?.first { $0.isEnabled }?.fieldOrder ?? .progressive).description)")
LabeledContent("Stream Type", value: (videoTracks?.first { $0.isEnabled }?.fieldOrder ?? .progressive).description)
if let dynamicInfo = config.playerLayer?.player.dynamicInfo {
Text("Display FPS: \(dynamicInfo.displayFPS) ")
Text("Audio Video sync: \(dynamicInfo.audioVideoSyncDiff)")
Text("Dropped Frames: \(dynamicInfo.droppedVideoFrameCount + dynamicInfo.droppedVideoPacketCount)")
Text("Bytes Read: \(dynamicInfo.bytesRead)")
Text("Audio bitrate: \(dynamicInfo.audioBitrate) b/s")
Text("Video bitrate: \(dynamicInfo.videoBitrate) b/s")
DynamicInfoView(dynamicInfo: dynamicInfo)
}
if let fileSize = config.playerLayer?.player.fileSize, fileSize > 0 {
Text("File Size: \(String(format: "%.1f", fileSize / 1_000_000))MB")
LabeledContent("File Size", value: fileSize.kmFormatted + "B")
}
}
.padding()
#if os(macOS) || targetEnvironment(macCatalyst)
.toolbar {
Button("Done") {
dismiss()
}
.keyboardShortcut(.defaultAction)
.toolbar {
Button("Done") {
dismiss()
}
.keyboardShortcut(.defaultAction)
}
#endif
}
}

@available(iOS 16, tvOS 16, macOS 13, *)
public struct DynamicInfoView: View {
@ObservedObject
fileprivate var dynamicInfo: DynamicInfo
public var body: some View {
LabeledContent("Display FPS", value: dynamicInfo.displayFPS, format: .number)
LabeledContent("Audio Video sync", value: dynamicInfo.audioVideoSyncDiff, format: .number)
LabeledContent("Dropped Frames", value: dynamicInfo.droppedVideoFrameCount + dynamicInfo.droppedVideoPacketCount, format: .number)
LabeledContent("Bytes Read", value: dynamicInfo.bytesRead.kmFormatted + "B")
LabeledContent("Audio bitrate", value: dynamicInfo.audioBitrate.kmFormatted + "bps")
LabeledContent("Video bitrate", value: dynamicInfo.videoBitrate.kmFormatted + "bps")
}
}

@available(iOS 15, tvOS 16, macOS 12, *)
public struct PlatformView<Content: View>: View {
private let content: () -> Content
Expand All @@ -561,6 +570,9 @@ public struct PlatformView<Content: View>: View {
Form {
content()
}
#if os(macOS)
.padding()
#endif
#endif
}

Expand Down

0 comments on commit 16472af

Please sign in to comment.