title: Struggling with NotificationCenter/Combine in SwiftUI/AVPlayer
body: <p>I'm trying to pause my AVPlayer when the item finishes playing. What is the best way to do this with SwiftUI? I don't know much about notifications, where to declare them, etc. Is there a way to use Combine for this? Sample code would be awesome! Thank you in advance.</p>
  
  <p>UPDATE:</p>
  
  <p>With help from the answer below, I managed to make a class which takes an AVPlayer and publishes a notification when the item ends. You can subscribe to the notification with the following:</p>
  
  <p>Class:</p>
  
  <pre><code>import Combine
  import AVFoundation
  
  class PlayerFinishedObserver {
  
      let publisher = PassthroughSubject&lt;Void, Never&gt;()
  
      init(player: AVPlayer) {
          let item = player.currentItem
  
          var cancellable: AnyCancellable?
          cancellable = NotificationCenter.default.publisher(for: .AVPlayerItemDidPlayToEndTime, object: item).sink { [weak self] change in
              self?.publisher.send()
              print("Gotcha")
              cancellable?.cancel()
          }
      }
  }
  </code></pre>
  
  <p>Add to your struct:</p>
  
  <pre><code>let finishedObserver: PlayerFinishedObserver
  </code></pre>
  
  <p>Subscribe on some View:</p>
  
  <pre><code>.onReceive(finishedObserver.publisher) {
                  print("Gotcha!")
              }
  </code></pre>