title: SwiftUI Video player how to set url
body: <p>I am trying to create a view that allows me to send a URL of the video I want to play. My view looks like this:</p>
  <pre><code>import SwiftUI
  import AVKit
  
  struct PlayerView: View {
      var videoURL : String
      private let player = AVPlayer(url: URL(string: &quot;https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8&quot;)!)
  
  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()
          }
      }
  }
  </code></pre>
  <p>If try to set the URL to the passed parameter I get this error:</p>
  <pre><code>Cannot use instance member 'videoURL' within property initializer; property initializers run     before 'self' is available
  </code></pre>
  <p>How do I pass aURL to the View to play different movies?</p>
  <hr />
  <p>I changed my code as suggested and the video does not play:</p>
  <pre><code>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()
          } 
      }
  }
  </code></pre>
  <p>I am trying to change the video on the timer. I tried this:</p>
  <pre><code>} else {
      VideoPlayer(player: viewModel.player)
      .onAppear {
            viewModel.urlString = &quot;https://testsite.neumont.edu/images/green.mp4&quot;
  
                          }
  
      .onReceive(movieSwitchTimer) { _ in
         DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
        viewModel.urlString = &quot;https://testsite.neumont.edu/images/earth.mp4&quot;
                              }
      self.activeImageIndex = (self.activeImageIndex + 1) % slideStore.images.count
           }
      }
  </code></pre>