VideoPlayer 自动播放 SwiftUI 问题
VideoPlayer Autoplay SwiftUI Issue
我正在尝试让 VideoPlayer
使用视频控制器自动播放,但我似乎无法弄明白!我应该怎么办?我是 SwiftUI 的新手。
struct VideoView: View {
// MARK: - PROPERTIES
//: BINDING
@Binding var videoView: Bool
//: STATE
@State var player = AVPlayer()
@State var isplaying = true
@State var showcontrols = true
//: VAR
var allowsPictureInPicturePlayback : Bool = true
var wrkflw: Wrkflw
// MARK: - BODY
var body: some View {
VideoPlayer(wrkflw: wrkflw, player: $player)
.edgesIgnoringSafeArea(.all)
.onAppear {
player.play()
}
}
}
标记:- 视频控制器
struct VideoPlayer : UIViewControllerRepresentable {
var wrkflw: Wrkflw
@Binding var player : AVPlayer
func makeUIViewController(context: Context) -> UIViewController {
let player = AVPlayer(url: wrkflw.wrkvideo)
let controller = AVPlayerViewController()
controller.player = player
controller.showsPlaybackControls = true
controller.exitsFullScreenWhenPlaybackEnds = true
controller.allowsPictureInPicturePlayback = true
return controller
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
最简单的解决方案是将 AVPlayer
引用从 VideoView
传递给 VideoPlayer
。您可以使用 player.replaceCurrentItem
以便您可以设置正确的 URL 但保持相同的播放器引用。
确保删除 VideoPlayer
中的 let player...
行
struct VideoView: View { // MARK: - PROPERTIES
//: BINDING
@Binding var videoView: Bool
//: STATE
@State var player = AVPlayer()
@State var isplaying = true
@State var showcontrols = true
//: VAR
var allowsPictureInPicturePlayback : Bool = true
var wrkflw: Wrkflw
// MARK: - BODY
var body: some View {
VideoPlayer(wrkflw: wrkflw, player: player)
.edgesIgnoringSafeArea(.all)
.onAppear {
player.replaceCurrentItem(with: AVPlayerItem(url: wrkflw.wrkvideo)) //<-- Here
player.play()
}
}
}
// MARK: - VIDEO CONTROLLER
struct VideoPlayer : UIViewControllerRepresentable {
var wrkflw: Wrkflw
var player : AVPlayer
func makeUIViewController(context: Context) -> UIViewController {
//<-- Here
let controller = AVPlayerViewController()
controller.player = player
controller.showsPlaybackControls = true
controller.exitsFullScreenWhenPlaybackEnds = true
controller.allowsPictureInPicturePlayback = true
return controller
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
我正在尝试让 VideoPlayer
使用视频控制器自动播放,但我似乎无法弄明白!我应该怎么办?我是 SwiftUI 的新手。
struct VideoView: View {
// MARK: - PROPERTIES
//: BINDING
@Binding var videoView: Bool
//: STATE
@State var player = AVPlayer()
@State var isplaying = true
@State var showcontrols = true
//: VAR
var allowsPictureInPicturePlayback : Bool = true
var wrkflw: Wrkflw
// MARK: - BODY
var body: some View {
VideoPlayer(wrkflw: wrkflw, player: $player)
.edgesIgnoringSafeArea(.all)
.onAppear {
player.play()
}
}
}
标记:- 视频控制器
struct VideoPlayer : UIViewControllerRepresentable {
var wrkflw: Wrkflw
@Binding var player : AVPlayer
func makeUIViewController(context: Context) -> UIViewController {
let player = AVPlayer(url: wrkflw.wrkvideo)
let controller = AVPlayerViewController()
controller.player = player
controller.showsPlaybackControls = true
controller.exitsFullScreenWhenPlaybackEnds = true
controller.allowsPictureInPicturePlayback = true
return controller
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
最简单的解决方案是将 AVPlayer
引用从 VideoView
传递给 VideoPlayer
。您可以使用 player.replaceCurrentItem
以便您可以设置正确的 URL 但保持相同的播放器引用。
确保删除 VideoPlayer
let player...
行
struct VideoView: View { // MARK: - PROPERTIES
//: BINDING
@Binding var videoView: Bool
//: STATE
@State var player = AVPlayer()
@State var isplaying = true
@State var showcontrols = true
//: VAR
var allowsPictureInPicturePlayback : Bool = true
var wrkflw: Wrkflw
// MARK: - BODY
var body: some View {
VideoPlayer(wrkflw: wrkflw, player: player)
.edgesIgnoringSafeArea(.all)
.onAppear {
player.replaceCurrentItem(with: AVPlayerItem(url: wrkflw.wrkvideo)) //<-- Here
player.play()
}
}
}
// MARK: - VIDEO CONTROLLER
struct VideoPlayer : UIViewControllerRepresentable {
var wrkflw: Wrkflw
var player : AVPlayer
func makeUIViewController(context: Context) -> UIViewController {
//<-- Here
let controller = AVPlayerViewController()
controller.player = player
controller.showsPlaybackControls = true
controller.exitsFullScreenWhenPlaybackEnds = true
controller.allowsPictureInPicturePlayback = true
return controller
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}