title: Xcode 11 and swift - background audio
body: <p>Every time I close the app (not quit it) or turn off the screen the music stops.
  is their something I am missing?</p>
  
  <p><a href="https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/creating_a_basic_video_player_ios_and_tvos/enabling_background_audio" rel="nofollow noreferrer">https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/creating_a_basic_video_player_ios_and_tvos/enabling_background_audio</a></p>
  
  <p>My code.</p>
  
  <p>I have a file called MediaPlayer.swift</p>
  
  <p>inside it I have the following</p>
  
  <pre><code>//
  //  MusicPlayer.swift
  //  DRN1
  //
  //  Created by Russell Harrower on 25/11/19.
  //  Copyright © 2019 Russell Harrower. All rights reserved.
  //
  
  import Foundation
  import AVFoundation
  
  
  class MusicPlayer {
      static let shared = MusicPlayer()
      var player: AVPlayer?
  
      func startBackgroundMusic() {
          let urlString = "http://stream.radiomedia.com.au:8003/stream"
          guard let url = URL.init(string: urlString)
              else {
                  return
          }
          let playerItem = AVPlayerItem.init(url: url)
          player = AVPlayer.init(playerItem: playerItem)
          player!.play()
      }
  }
  </code></pre>
  
  <p>I am calling that from my FirstViewController.swift</p>
  
  <pre><code>override func viewDidLoad() {
  
            MusicPlayer.shared.startBackgroundMusic()
            // Do any additional setup after loading the view.
            self.artist.textAlignment = .center
            self.song.textAlignment = .center
            scheduledTimerWithTimeInterval()
            self.nowplaying()
  
  
  
        }
  </code></pre>
  
  <p>I have the following ticked.</p>
  
  <p><a href="https://i.stack.imgur.com/g82VR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g82VR.png" alt="Background"></a></p>
  
  <p>here a demo of it currently <a href="https://www.youtube.com/watch?v=REtKHUR13k0" rel="nofollow noreferrer">https://www.youtube.com/watch?v=REtKHUR13k0</a></p>