eroscai / SZAVPlayer

SZAVPlayer is a lightweight audio/video player library, based on AVPlayer and AVAssetResourceLoaderDelegate, pure-Swift. Support cache and video image output.
MIT License
244 stars 32 forks source link

Using player inside resizable collection view cell #10

Closed binho closed 3 years ago

binho commented 3 years ago

I've been trying to use the player inside a collection view cell that can change it's width/height based on user collapsing/expanding it but look like the video player layer is not respecting the player constraints.

Kapture 2021-08-13 at 09 52 23 (I've added a green border to the video player to make it easy to see)

Tried some things from the cell itself like:

// tried on `playerLayer` and `playerView` itself (playerView is an instance of `SZAVPlayer`)
override func layoutSubviews() {
    super.layoutSubviews()
    playerView.setNeedsLayout()
    playerView.layoutIfNeeded()
}

but only way to fix it for now was update SZAVPlayer and add:

public override func layoutSubviews() {
    super.layoutSubviews()

    playerLayer?.frame = bounds
    playerLayer?.removeAllAnimations()
}

Not sure it's a bug or is there a simpler way to fix it without modifying the source. thanks

eroscai commented 3 years ago

SZAVPlayer has set the parameter playerLayer as public access. https://github.com/eroscai/SZAVPlayer/blob/786b5337cae31ba1225514329b3d4b96d49bbc2e/Sources/Classes/SZAVPlayer.swift#L100

You can use it directly to achieve your purpose. For example

// tried on `playerLayer` and `playerView` itself (playerView is an instance of `SZAVPlayer`)
override func layoutSubviews() {
    super.layoutSubviews()
    playerView.playerLayer?.frame = bounds
    playerView.playerLayer?.layoutIfNeeded()
}

If is still doesn't working after trying, please tell me.

binho commented 3 years ago

@eroscai It worked, I just changed layoutIfNeeded() to removeAllAnimations() to prevent it from animating.