SwiftUI视频播放器如何设置url

SwiftUI Video player how to set url

我正在尝试创建一个允许我发送 URL 我想播放的视频的视图。我的观点是这样的:

import SwiftUI
import AVKit

struct PlayerView: View {
    var videoURL : String
    private let player = AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!)

var body: some View {
    VideoPlayer(player: player)
        .onAppear() {
            // Start the player going, otherwise controls don't appear
            player.play()
        }
        .onDisappear() {
            // Stop the player when the view disappears
            player.pause()
        }
    }
}

如果尝试将 URL 设置为传递的参数,我会收到此错误:

Cannot use instance member 'videoURL' within property initializer; property initializers run     before 'self' is available

如何将 URL 传递给视图以播放不同的电影?


我按照建议更改了代码,但视频无法播放:

import SwiftUI
import AVKit

struct PlayerView: View {
    var videoURL : String
    @State private var player : AVPlayer?

var body: some View {
    VideoPlayer(player: player)
        .onAppear() {
            // Start the player going, otherwise controls don't appear
            guard let url = URL(string: videoURL) else {
                return
            }
            print(url)
            let player = AVPlayer(url: url)
            self.player = player
            self.player?.seek(to: CMTime.zero)
            self.player!.play()
        }
        .onDisappear() {
            // Stop the player when the view disappears
            player?.pause()
        } 
    }
}

我正在尝试更改计时器上的视频。我试过这个:

} else {
    VideoPlayer(player: viewModel.player)
    .onAppear {
          viewModel.urlString = "https://testsite.neumont.edu/images/green.mp4"

                        }

    .onReceive(movieSwitchTimer) { _ in
       DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
      viewModel.urlString = "https://testsite.neumont.edu/images/earth.mp4"
                            }
    self.activeImageIndex = (self.activeImageIndex + 1) % slideStore.images.count
         }
    }

有多种方法可以实现此目的,但由于您已经在使用 onAppear,这似乎是实现此目的的好地方:

struct PlayerView: View {
    var videoURL : String
    @State private var player : AVPlayer?
    
    var body: some View {
        VideoPlayer(player: player)
            .onAppear() {
                // Start the player going, otherwise controls don't appear
                guard let url = URL(string: videoURL) else {
                    return
                }
                let player = AVPlayer(url: url)
                self.player = player
                player.play()
            }
            .onDisappear() {
                // Stop the player when the view disappears
                player?.pause()
            }
    }
}

您希望 player 成为 @State,因为它应该在视图渲染之间持续存在。

更新,根据评论,处理URL可能改变的场景

class VideoViewModel : ObservableObject {
    @Published var urlString : String? {
        didSet {
            guard let urlString = urlString, let url = URL(string: urlString) else {
                return
            }
            player = AVPlayer(url: url)
            player.seek(to: .zero)
            player.play()
        }
    }
    var player = AVPlayer()
}

struct ContentView : View {
    @StateObject var viewModel = VideoViewModel()
    
    var body: some View {
        VideoPlayer(player: viewModel.player)
            .onAppear {
                viewModel.urlString = "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"
                DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
                    viewModel.urlString = Bundle.main.url(forResource: "IMG_0226", withExtension: "mp4")!.absoluteString
                }
            }
    }
}

在更新中,您可以看到我将播放器存储在 ObservableObject 中。每当 url 字符串发生变化时,它都会重新加载播放器。 onAppear 在您的代码中不是必需的 - 它只是一种显示在不同时间加载不同视频的方式(第一个是您提供的 URL,第二个是我的 URL捆绑)。