swiftui 2中的背景视频
Background video in swiftui 2
我正在尝试使用 swifui 2 和 IOS 14
在后台播放视频
我的目标是让带有播放按钮的栏完全消失。
通常使用 .disabled (true) 条应该消失,但情况并非如此,它始终出现在视频开始处,并在播放开始后约 1 秒消失。
import SwiftUI
import AVKit
struct TestBackground: View {
private let player = AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!)
private let controler = AVPlayerViewController()
init() {
let control = AVPlayerViewController()
control.showsPlaybackControls = true
}
var body: some View {
VideoPlayer(player: player)
.aspectRatio(contentMode: .fill)
.edgesIgnoringSafeArea(.all)
.disabled(true)
.onAppear() {
player.isMuted = true
player.play()
}
.onDisappear() {
// Stop the player when the view disappears
player.pause()
}
.scaledToFill()
}
}
struct TestBackground_Previews: PreviewProvider {
static var previews: some View {
TestBackground()
}
}
VideoPlayer
不公开属性以隐藏控件。但是,AVPlayerViewController
会。它需要包裹在 UIViewControllerRepresented
中,但是:
struct AVPlayerControllerRepresented : UIViewControllerRepresentable {
var player : AVPlayer
func makeUIViewController(context: Context) -> AVPlayerViewController {
let controller = AVPlayerViewController()
controller.player = player
controller.showsPlaybackControls = false
return controller
}
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
}
}
struct TestBackground: View {
private let player = AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!)
var body: some View {
AVPlayerControllerRepresented(player: player)
.edgesIgnoringSafeArea(.all)
.disabled(true)
.onAppear() {
player.isMuted = true
player.play()
}
.onDisappear() {
// Stop the player when the view disappears
player.pause()
}
.scaledToFill()
}
}
在您的原始代码中,您在 init
中创建了一个 AVPlayerViewController
,但从未实际使用它。
我正在尝试使用 swifui 2 和 IOS 14
在后台播放视频我的目标是让带有播放按钮的栏完全消失。
通常使用 .disabled (true) 条应该消失,但情况并非如此,它始终出现在视频开始处,并在播放开始后约 1 秒消失。
import SwiftUI
import AVKit
struct TestBackground: View {
private let player = AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!)
private let controler = AVPlayerViewController()
init() {
let control = AVPlayerViewController()
control.showsPlaybackControls = true
}
var body: some View {
VideoPlayer(player: player)
.aspectRatio(contentMode: .fill)
.edgesIgnoringSafeArea(.all)
.disabled(true)
.onAppear() {
player.isMuted = true
player.play()
}
.onDisappear() {
// Stop the player when the view disappears
player.pause()
}
.scaledToFill()
}
}
struct TestBackground_Previews: PreviewProvider {
static var previews: some View {
TestBackground()
}
}
VideoPlayer
不公开属性以隐藏控件。但是,AVPlayerViewController
会。它需要包裹在 UIViewControllerRepresented
中,但是:
struct AVPlayerControllerRepresented : UIViewControllerRepresentable {
var player : AVPlayer
func makeUIViewController(context: Context) -> AVPlayerViewController {
let controller = AVPlayerViewController()
controller.player = player
controller.showsPlaybackControls = false
return controller
}
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
}
}
struct TestBackground: View {
private let player = AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!)
var body: some View {
AVPlayerControllerRepresented(player: player)
.edgesIgnoringSafeArea(.all)
.disabled(true)
.onAppear() {
player.isMuted = true
player.play()
}
.onDisappear() {
// Stop the player when the view disappears
player.pause()
}
.scaledToFill()
}
}
在您的原始代码中,您在 init
中创建了一个 AVPlayerViewController
,但从未实际使用它。