title: Let AVPlayer continue playing when pressing the 'back' button on the UINavigation Controller in Swift 2
body: <p>I’m developing a Radio Streaming app, it streams OK. Also it plays in background when press on the ‘Home’ button and ‘lock’ button. </p>
  
  <p>The application is embedded into a <code>UINavigationController</code> and when I press on the ‘back’ button in the UINavigationController it stops playing. 
  My question is: How do I let the <code>UIViewController</code> that contain the <code>AVPlayer</code> to remain active when pressing the ‘back’ button in the navigation controller so that the <code>AVPlayer</code> continue streaming?</p>
  
  <p>My code</p>
  
  <pre><code>import UIKit
  import AVFoundation
  import MediaPlayer
  import Foundation
  
  class RadioFunctionViewController: UIViewController {
  
  @IBOutlet var playButton: UIButton!
  @IBOutlet var statusLabel: UILabel!
  var player:AVPlayer = AVPlayer()
  private let ObservatingKeyPath = "currentItem.status"
  private let PlayerStatusObservingContext = UnsafeMutablePointer&lt;Int&gt;(bitPattern: 1)
  private var playingState:Bool = false
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      setStatus(4)
      getAudioData("http://184.107.179.162:7546/;")
      playingState = true
  
  
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  
  @IBAction func buttonPressed(sender: AnyObject)
  {
      toggle()
  }
  
  func getAudioData(audioURL:String)
  {
      player = AVPlayer(URL: NSURL(string: audioURL)!)
      player.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.Initial, context: nil)
  }
  
  func setStatus(rawValue:Int)
  {
      if rawValue == 1
      {
          statusLabel.textColor = UIColor.blueColor()
          statusLabel.text = "Ready for Streaming"
      }else if rawValue == 2
      {
          statusLabel.textColor = UIColor.redColor()
          statusLabel.text = "Failed"
  
      }else if rawValue == 0
      {
          statusLabel.textColor = UIColor.redColor()
          statusLabel.text = "Failed to load data"
      }else if rawValue == 3
      {
          statusLabel.textColor = UIColor.blueColor()
          statusLabel.text = "Streaming"
      }else if rawValue == 4
      {
          statusLabel.textColor = UIColor.purpleColor()
          statusLabel.text = "Gather data..."
      }
      print("The raw value send is: \(rawValue)")
  }
  
  func audioBackgroundPlayback()
  {
      do{
          try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
      }catch {
          print("Could not play audio in the background")
      }
  
      if (NSClassFromString("MPNowPlayingInfoCenter") != nil)
      {
          let artWorkImage = MPMediaItemArtwork(image: UIImage(named: "ws")!)
          let songInfo2: [String:  AnyObject] = [MPMediaItemPropertyTitle: "Wide Streamings ABC Edition", MPMediaItemPropertyArtist: "Rumbera Network", MPMediaItemPropertyAlbumTitle: "107.9 FM", MPMediaItemPropertyArtwork: artWorkImage]
  
          MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo2
          UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
      }
  
  
  }
  
  func toggle()
  {
      if playButton.titleLabel?.text == "Play"
      {
          print("The play option is chosen")
          playRadio()
      }else{
          print("The pause option is chosen")
          pauseRadio()
      }
  }
  
  func playRadio()
  {
      player.play()
      setStatus(3)
      playButton.setTitle("Pause", forState: UIControlState.Normal)
      audioBackgroundPlayback()
  }
  
  func pauseRadio()
  {
      player.pause()
      playButton.setTitle("Play", forState: UIControlState.Normal)
  }
  
  override func remoteControlReceivedWithEvent(event: UIEvent?) {
      if event?.type == UIEventType.RemoteControl
      {
          if event?.subtype == UIEventSubtype.RemoteControlPlay
          {
              toggle()
          }else if event?.subtype == UIEventSubtype.RemoteControlPause
          {
              pauseRadio()
          }else if event?.subtype == UIEventSubtype.RemoteControlTogglePlayPause
          {
              toggle()
          }
      }
  }
  
  override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer&lt;Void&gt;)
  {
      if (keyPath!.containsString("status"))
      {
          if player.status == AVPlayerStatus.ReadyToPlay
          {
              player.prerollAtRate(0.001, completionHandler: {(succes:Bool)-&gt; Void in
  
                  if succes{
                      self.setStatus(1)
                      self.setStatus(3)
                      self.playRadio()
  
                  }else{
                      self.setStatus(1)
                      self.setStatus(2)
                  }
  
              })
          }else if player.status == AVPlayerStatus.Failed{
              self.setStatus(2)
  
          }else if player.status == AVPlayerStatus.Unknown
          {
              self.setStatus(0)
          }
  
      }
  }
  
  override func viewDidAppear(animated: Bool) {
      super.viewDidAppear(animated)
      self.navigationController?.navigationBarHidden = false
  }
  
  override func viewWillDisappear(animated: Bool) {
      super.viewWillDisappear(animated)
      self.navigationController?.navigationBarHidden = false
      if playingState == true
      {
          audioBackgroundPlayback()
          player.removeObserver(self, forKeyPath: "status")
          print("The AVPlayer is playing in background")
      }else{
          player.removeObserver(self, forKeyPath: "status")
          print("The view Dissapear")
      }
  
  
  }
  </code></pre>
  
  <p>Hope someone could help me out with this one
  Thanks in advance</p>