gpt4 book ai didi

swift - 带有 SwiftUI 的 VLCKit

转载 作者:行者123 更新时间:2023-12-04 04:19:13 40 4
gpt4 key购买 nike

我想用 VLCKit SwiftUI .

为什么不是 AVPlayer?因为不支持某些视频格式。

这是我的代码:

import SwiftUI
import UIKit

struct ContentView: View {
var body: some View {
NavigationView
{
VStack{
PlayerView(url: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")
}
}
}
}
struct PlayerView: UIViewRepresentable {
public var url: String
let mediaPlayer = VLCMediaPlayer()
func makeUIView(context: Context) -> UIView {

let controller = UIView()
mediaPlayer.drawable = controller
let uri = URL(string: self.url)
let media = VLCMedia(url: uri!)
mediaPlayer.media = media
mediaPlayer.play()
return controller
}

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

这有效并加载视频,但我想添加播放控件(全屏、播放/暂停、前进等)。

我没有找到任何关于这方面的文件,谁能帮我一下,

谢谢

最佳答案

我在优酷上看过这个视频
https://www.youtube.com/watch?v=N_u9nsXNvn4

和这个
How to show my AVPlayer in a VStack with SwiftUI

这是我的演示

import SwiftUI

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

func makeUIView(context: Context) -> UIView {
return PlayerUIView(frame: .zero)
}
}

class PlayerUIView: UIView, VLCMediaPlayerDelegate {
private let mediaPlayer = VLCMediaPlayer()

override init(frame: CGRect) {
super.init(frame: frame)

let url = URL(string: "rtsp://xxxxx")!//replace your resource here

mediaPlayer.media = VLCMedia(url: url)
mediaPlayer.delegate = self
mediaPlayer.drawable = self
mediaPlayer.play()
}

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

override func layoutSubviews() {
super.layoutSubviews()
}
}

struct VlcPlayerDemo_Previews: PreviewProvider {
static var previews: some View {
VlcPlayerDemo()
}
}

关于swift - 带有 SwiftUI 的 VLCKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59806850/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com