Skip to content

Commit

Permalink
refactor aspectFill
Browse files Browse the repository at this point in the history
  • Loading branch information
LePips committed Sep 20, 2022
1 parent 88b6afe commit 71fcc74
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
16 changes: 12 additions & 4 deletions Sources/VLCUI/Extensions/CGSizeExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ extension CGSize {

static func aspectFill(aspectRatio: CGSize, minimumSize: CGSize) -> CGSize {
var minimumSize = minimumSize
let mW = minimumSize.width / aspectRatio.width
let mH = minimumSize.height / aspectRatio.height
let widthRatio = minimumSize.width / aspectRatio.width
let heightRatio = minimumSize.height / aspectRatio.height

if mH > mW {
if heightRatio > widthRatio {
minimumSize.width = minimumSize.height / aspectRatio.height * aspectRatio.width
} else if mW > mH {
} else if widthRatio > heightRatio {
minimumSize.height = minimumSize.width / aspectRatio.width * aspectRatio.height
}

return minimumSize
}

func scale(other: CGSize) -> CGFloat {
if height > other.height {
return height / other.height
} else {
return width / other.width
}
}
}
57 changes: 22 additions & 35 deletions Sources/VLCUI/UIVLCVideoPlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public class UIVLCVideoPlayerViewController: UIViewController {
private var lastPlayerState: VLCMediaPlayerState = .opening
private var cancellables = Set<AnyCancellable>()

private var aspectFillScale: CGFloat {
guard let currentMediaPlayer = currentMediaPlayer else { return 1 }
let videoSize = currentMediaPlayer.videoSize
let fillSize = CGSize.aspectFill(aspectRatio: videoSize, minimumSize: videoContentView.bounds.size)
return fillSize.scale(other: videoContentView.bounds.size)
}

init(
configuration: VLCVideoPlayer.Configuration,
delegate: VLCVideoPlayerDelegate
Expand Down Expand Up @@ -64,7 +71,6 @@ public class UIVLCVideoPlayerViewController: UIViewController {

private func setupVLCMediaPlayer(with configuration: VLCVideoPlayer.Configuration) {
self.currentMediaPlayer?.stop()
self.currentMediaPlayer?.delegate = nil
self.currentMediaPlayer = nil

let media = VLCMedia(url: configuration.url)
Expand Down Expand Up @@ -131,11 +137,9 @@ public extension UIVLCVideoPlayerViewController {
let newSpeed = currentMediaPlayer.fastForwardSpeed(from: speed)
currentMediaPlayer.fastForward(atRate: newSpeed)
case let .aspectFill(fill):
if fill {
self.fillScreen(screenSize: self.videoContentView.bounds.size)
} else {
self.shrinkScreen()
}
guard fill >= 0 && fill <= 1 else { return }
let scale = 1 + CGFloat(fill) * (self.aspectFillScale - 1)
self.videoContentView.transform = CGAffineTransform(scaleX: scale, y: scale)
case let .setTime(time):
guard time.asTicks >= 0 && time.asTicks <= media.length.intValue else { return }
currentMediaPlayer.time = VLCTime(int: time.asTicks)
Expand All @@ -153,30 +157,6 @@ public extension UIVLCVideoPlayerViewController {
}
.store(in: &cancellables)
}

private func fillScreen(screenSize: CGSize) {
guard let currentMediaPlayer = currentMediaPlayer else { return }
let videoSize = currentMediaPlayer.videoSize
let fillSize = CGSize.aspectFill(aspectRatio: videoSize, minimumSize: screenSize)

let scale: CGFloat

if fillSize.height > screenSize.height {
scale = fillSize.height / screenSize.height
} else {
scale = fillSize.width / screenSize.width
}

UIView.animate(withDuration: 0.2) {
self.videoContentView.transform = CGAffineTransform(scaleX: scale, y: scale)
}
}

private func shrinkScreen() {
UIView.animate(withDuration: 0.2) {
self.videoContentView.transform = .identity
}
}
}

// MARK: VLCMediaPlayerDelegate
Expand Down Expand Up @@ -256,6 +236,18 @@ extension UIVLCVideoPlayerViewController: VLCMediaPlayerDelegate {
}

private func setDefaultConfiguration(with player: VLCMediaPlayer, from configuration: VLCVideoPlayer.Configuration) {

player.time = VLCTime(int: configuration.startTime.asTicks)

let defaultPlayerSpeed = player.fastForwardSpeed(from: configuration.playbackSpeed)
player.fastForward(atRate: defaultPlayerSpeed)

if configuration.aspectFill {
self.videoContentView.transform = CGAffineTransform(scaleX: aspectFillScale, y: aspectFillScale)
} else {
self.videoContentView.transform = .identity
}

let defaultSubtitleTrackIndex = player.subtitleTrackIndex(from: configuration.subtitleIndex)
player.currentVideoSubTitleIndex = defaultSubtitleTrackIndex

Expand All @@ -265,10 +257,5 @@ extension UIVLCVideoPlayerViewController: VLCMediaPlayerDelegate {
player.setSubtitleSize(configuration.subtitleSize)
player.setSubtitleFont(configuration.subtitleFont)
player.setSubtitleColor(configuration.subtitleColor)

player.time = VLCTime(int: configuration.startTime.asTicks)

let defaultPlayerSpeed = player.fastForwardSpeed(from: configuration.playbackSpeed)
player.fastForward(atRate: defaultPlayerSpeed)
}
}
1 change: 1 addition & 0 deletions Sources/VLCUI/VLCVideoPlayer/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public extension VLCVideoPlayer {
public var url: URL
public var autoPlay: Bool = false
public var startTime: TimeSelector = .ticks(0)
public var aspectFill: Bool = false
public var playbackSpeed: ValueSelector<Float> = .auto
public var subtitleIndex: ValueSelector<Int32> = .auto
public var audioIndex: ValueSelector<Int32> = .auto
Expand Down
5 changes: 3 additions & 2 deletions Sources/VLCUI/VLCVideoPlayer/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public extension VLCVideoPlayer {
/// Fast forward at a given rate
case fastForward(ValueSelector<Float>)

/// Aspect fill depending on the video's content size and the view's bounds
case aspectFill(Bool)
/// Aspect fill depending on the video's content size and the view's bounds, based
/// on the given percentage of completion
case aspectFill(Float)

/// Set the player time
case setTime(TimeSelector)
Expand Down

0 comments on commit 71fcc74

Please sign in to comment.