我目前有一个视频播放器,可以播放循环播放的视频一定时间。 我现在想在每个循环之间添加一个平滑的过渡,例如模糊。有人知道如何使用我当前的代码吗?是否有可以轻松集成到我的代码或属性中的功能? 感谢任何帮助!
import SwiftUI
import AVFoundation
// MARK: ExerciseVideoPlayer: UIViewRepresentable
struct ExerciseVideoPlayer: UIViewRepresentable {
var increaseRepetitions: () -> Void
var videoUrl: URL
func makeUIView(context: Context) -> UIView {
LoopingVideoPlayerView(repetitions: increaseRepetitions, videoUrl: videoUrl)
}
func updateUIView(_ uiView: UIView, context: Context) {
// Do nothing here
}
}
// MARK: LoopingVideoPlayerView: UIView
class LoopingVideoPlayerView: UIView {
// MARK: Stored properties
var increaseRepetitions: () -> Void
var videoUrl: URL
private var playerLayer = AVPlayerLayer()
private var playerLooper: AVPlayerLooper?
// MARK: Initialization
init(repetitions: @escaping () -> Void, videoUrl: URL) {
self.increaseRepetitions = repetitions
self.videoUrl = videoUrl
super.init(frame: .zero)
// Load Video
let playerItem = AVPlayerItem(url: videoUrl)
// Setup Player
let player = AVQueuePlayer(playerItem: playerItem)
playerLayer.player = player
playerLayer.videoGravity = .resizeAspect
layer.addSublayer(playerLayer)
// Loop
playerLooper = AVPlayerLooper(player: player, templateItem: playerItem) //evtl. time range?
// Play
//discuss: Stop video loop after number of desired repetitions is reached
NotificationCenter.default.addObserver(self,
selector: #selector(self.increaseRepetitionCount),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: nil)
player.play()
}
// MARK: Methods
override func layoutSubviews() {
super.layoutSubviews()
playerLayer.frame = bounds
}
// MARK: Whatever that is
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func increaseRepetitionCount() {
increaseRepetitions()
}
}
// MARK: ExerciseVideoPlayer_Previews: PreviewProvider
struct ExerciseVideoPlayer_Previews: PreviewProvider {
static var previews: some View {
guard let videoUrl = URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_10MB.mp4") else {
fatalError("videoURL not found")
}
return ExerciseVideoPlayer(increaseRepetitions: {}, videoUrl: videoUrl)
}
}