0

I've created an app for playing radio stream from URL, but it is not playing audio when app is on background. I checked audio and airplay in background modes in app settings but still it does not work. Do I need to add some permissions in appdelegate? Any ideas? Thank you in advance for your help. Here is my code:

import UIKit
import AVKit



class ViewController: UIViewController {
    
    
    
    var player : AVPlayer!
    var dict = NSDictionary()
    
   
    @IBOutlet weak var ArtistLabel: UILabel!
    
    
    @IBAction func playButtonPressed(_ sender: UIButton){
        let url = "https://test.com/stream.mp3"
            player = AVPlayer(url: URL(string: url)!)
            player.volume = 1.0
            player.rate = 1.0
            player.play()
        
    }
    
    
    
    @IBAction func stopButtonStopped(sender: UIButton) {
        player.pause()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        overrideUserInterfaceStyle = .light
       
    }
   

}
LewTrocki
  • 41
  • 7
  • Does this answer your question? [How to play audio in background with Swift?](https://stackoverflow.com/questions/30280519/how-to-play-audio-in-background-with-swift) – vpoltave Sep 18 '20 at 12:18
  • See the second link in the first answer of the duplicate question. (https://developer.apple.com/library/ios/qa/qa1626/_index.html) – Duncan C Sep 18 '20 at 12:26

1 Answers1

2

You need to set your app Capabilities Background Modes (Audio and AirPlay) and set your AVAudioSession category to AVAudioSessionCategoryPlayback and set it active. Please Refer to this link below : How to play audio in background with Swift?

shafi
  • 426
  • 3
  • 15