【问题标题】:Background video in swiftui 2swiftui 2中的背景视频
【发布时间】:2021-03-24 17:21:24
【问题描述】:

我正在尝试使用 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()
    }
}

【问题讨论】:

标签: ios swift background swiftui ios14


【解决方案1】:

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,但从未实际使用它。

【讨论】:

  • 非常感谢,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 2022-07-15
  • 2015-01-10
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多