如何使用 SwiftUI 在 VStack 中显示我的 AVPlayer

Fra*_*bec 6 avplayer swiftui

我创建了一个简单的 AVPlayer。(如果我没有正确创建它,请帮我修复它...)我想用 swift ui 在我的 VStack 中显示它,但我有点卡住了...

要是库里至少有AVPlayer View组件就好了,但是我在库里没找到……

这是我的代码ContentView.swift

//
//  ContentView.swift
//  test
//
//  Created by Francis Dolbec on 2019-06-26.
//  Copyright © 2019 Francis Dolbec. All rights reserved.
//

import SwiftUI
import AVKit
import AVFoundation

// MARK: variables
var hauteurMenuBar = NSApplication.shared.mainMenu?.menuBarHeight
var urlVideo = URL(string: "/Users/francisdolbec/Movies/Se?ries Te?le?/Rick et Morty/Rick.and.Morty.S01E01.VFQ.HDTV.1080p.x264-Kamek.mp4")
let player = AVPlayer(url: urlVideo!)


struct ContentView : View {
    var body: some View {

        VStack {
            Text("Hello World")
                .frame(maxWidth: .infinity, maxHeight: .infinity)
            Text("PATATE!")
            //player.play()
            }

        .frame(minWidth: 1024, idealWidth: 1440, maxWidth: .infinity, minHeight: (640-hauteurMenuBar!), idealHeight: (900-hauteurMenuBar!), maxHeight: .infinity)

    }
}


#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif
Run Code Online (Sandbox Code Playgroud)

顺便说一句,如果有一个教程展示如何使用 swift ui 制作视频播放器,我将非常感激!

编辑 我忘了说我正在开发一个macOS应用程序。

Bog*_*rca 11

当然,这是Chris Mash 在 Medium上的教程。

基本上,您将 an 嵌入AVPlayerLayer到符合 的结构中UIViewRepresentable,您将对UIView要与 SwiftUI 一起使用的任何组件执行此操作。

struct PlayerView: UIViewRepresentable {
  func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PlayerView>) {
  }

  func makeUIView(context: Context) -> UIView {
    return PlayerUIView(frame: .zero)
  }
}
Run Code Online (Sandbox Code Playgroud)

实现的“肉”在PlayerUIView类中完成:

class PlayerUIView: UIView {
  private let playerLayer = AVPlayerLayer()

  override init(frame: CGRect) {
    super.init(frame: frame)
    
    let url = URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!
    let player = AVPlayer(url: url)
    player.play()
    
    playerLayer.player = player
    layer.addSublayer(playerLayer)
  }

  required init?(coder: NSCoder) {
     fatalError("init(coder:) has not been implemented")
  }

  override func layoutSubviews() {
    super.layoutSubviews()
    playerLayer.frame = bounds
  }
}
Run Code Online (Sandbox Code Playgroud)

然后你像这样使用它:

var body: some View {
  PlayerView()
}
Run Code Online (Sandbox Code Playgroud)


Fra*_*bec 4

感谢 Bogdan 的快速回答和对教程的喜欢!

这是转换为 NSView 的代码,因此它可以适用于 macOS 应用程序...

这是结构PlayerView

struct PlayerView: NSViewRepresentable {
    func updateNSView(_ nsView: NSView, context: NSViewRepresentableContext<PlayerView>) {

    }
    func makeNSView(context: Context) -> NSView {
        return PlayerNSView(frame: .zero)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是课程,就像博格丹所说的“肉”:

class PlayerNSView: NSView{
    private let playerLayer = AVPlayerLayer()

    override init(frame:CGRect){
        super.init(frame: frame)
        let urlVideo = URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!
        let player = AVPlayer(url: urlVideo)
        player.play()
        playerLayer.player = player
        if layer == nil{
            layer = CALayer()
        }
        layer?.addSublayer(playerLayer)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layout() {
        super.layout()
        playerLayer.frame = bounds
    }
}
Run Code Online (Sandbox Code Playgroud)

最后你可以使用PlayerView()添加AVPlayer到struct ContentView.