title: AVPlayer audio buffering in swift 3 source disconnected observer
body: <p>I have app that plays AAC audio stream. Everything works fine, but when I disconnect stream and connect again after one second audio stop playing after half minute. When i don't reconnect i have error after one- two minutes. To reconnect i must stop AVPlayer and start again. I want to reconnect stream or show message immediately after player stops play music. How can I do that? Moreover i have another question: I convert my code to swift 3 and I have problem with one line:</p>
  
  <pre><code>fileprivate var playerItem = AVPlayerItem?()
  </code></pre>
  
  <p>error: cannot invoke initializer without argument
  How i can fix that? Maybe this is the problem?</p>
  
  <p>My Radio Player class:</p>
  
  <pre><code>import Foundation
  import AVFoundation
  import UIKit
  
  protocol errorMessageDelegate {
      func errorMessageChanged(_ newVal: String)
  }
  
  protocol sharedInstanceDelegate {
      func sharedInstanceChanged(_ newVal: Bool)
  }
  
  class RadioPlayer : NSObject {
      static let sharedInstance = RadioPlayer()
      var instanceDelegate:sharedInstanceDelegate? = nil
      var sharedInstanceBool = false {
          didSet {
              if let delegate = self.instanceDelegate {
                  delegate.sharedInstanceChanged(self.sharedInstanceBool)
              }
          }
      }
      fileprivate var player = AVPlayer(url: URL(string: Globals.radioURL)!)
   //   fileprivate var playerItem = AVPlayerItem?()
      fileprivate var isPlaying = false
  
      var errorDelegate:errorMessageDelegate? = nil
      var errorMessage = "" {
          didSet {
              if let delegate = self.errorDelegate {
                  delegate.errorMessageChanged(self.errorMessage)
              }
          }
      }
  
      override init() {
          super.init()
  
          errorMessage = ""
  
          let asset: AVURLAsset = AVURLAsset(url: URL(string: Globals.radioURL)!, options: nil)
  
          let statusKey = "tracks"
  
          asset.loadValuesAsynchronously(forKeys: [statusKey], completionHandler: {
              var error: NSError? = nil
  
              DispatchQueue.main.async(execute: {
                  let status: AVKeyValueStatus = asset.statusOfValue(forKey: statusKey, error: &amp;error)
  
                  if status == AVKeyValueStatus.loaded{
  
                      let playerItem = AVPlayerItem(asset: asset)
  
                      self.player = AVPlayer(playerItem: playerItem)
                      self.sharedInstanceBool = true
  
                  } else {
                      self.errorMessage = error!.localizedDescription
                      print(error!)
                  }
  
              })
  
  
          })
  
          NotificationCenter.default.addObserver(
              forName: NSNotification.Name.AVPlayerItemFailedToPlayToEndTime,
              object: nil,
              queue: nil,
              using: { notification in
                  print("Status: Failed to continue")
                  self.errorMessage = NSLocalizedString("STREAM_INTERUPT", comment:"Stream was interrupted")
          })
  
          print("Initializing new player")
  
      }
  
      func resetPlayer() {
          errorMessage = ""
  
          let asset: AVURLAsset = AVURLAsset(url: URL(string: Globals.radioURL)!, options: nil)
  
          let statusKey = "tracks"
  
          asset.loadValuesAsynchronously(forKeys: [statusKey], completionHandler: {
              var error: NSError? = nil
  
              DispatchQueue.main.async(execute: {
                  let status: AVKeyValueStatus = asset.statusOfValue(forKey: statusKey, error: &amp;error)
  
                  if status == AVKeyValueStatus.loaded{
  
                      let playerItem = AVPlayerItem(asset: asset)
            //          playerItem.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.New, context: &amp;ItemStatusContext)
  
                      self.player = AVPlayer(playerItem: playerItem)
                      self.sharedInstanceBool = true
  
                  } else {
                      self.errorMessage = error!.localizedDescription
                      print(error!)
                  }
  
              })
          })
      }
  
      func bufferFull() -&gt; Bool {
          return bufferAvailableSeconds() &gt; 45.0
      }
  
      func bufferAvailableSeconds() -&gt; TimeInterval {
          // Check if there is a player instance
          if ((player.currentItem) != nil) {
  
              // Get current AVPlayerItem
              let item: AVPlayerItem = player.currentItem!
              if (item.status == AVPlayerItemStatus.readyToPlay) {
  
                  let timeRangeArray: NSArray = item.loadedTimeRanges as NSArray
                  if timeRangeArray.count &lt; 1 { return(CMTimeGetSeconds(kCMTimeInvalid)) }
                  let aTimeRange: CMTimeRange = (timeRangeArray.object(at: 0) as AnyObject).timeRangeValue
           //       let startTime = CMTimeGetSeconds(aTimeRange.end)
                  let loadedDuration = CMTimeGetSeconds(aTimeRange.duration)
  
                  return (TimeInterval)(loadedDuration);
              }
              else {
                  return(CMTimeGetSeconds(kCMTimeInvalid))
              }
          } 
          else {
              return(CMTimeGetSeconds(kCMTimeInvalid))
          }
      }
  
      func play() {
          player.play()
          isPlaying = true
          print("Radio is \(isPlaying ? "" : "not ")playing")
      }
  
      func pause() {
          player.pause()
          isPlaying = false
          print("Radio is \(isPlaying ? "" : "not ")playing")
      }
  
      func currentlyPlaying() -&gt; Bool {
          return isPlaying
      }
  }
  </code></pre>
  
  <p>I will be grateful for help ;)</p>