Music playing stopped in the background mode?

Hi,


I have developed a Music app that is intended to play music in the background also. For this, I enabled the audio play option from background modes from capabilities. I have bundled two audio files to play music. On the app login music started playing and in the backgroud calling API which has almost 10000 songs json data(urls and other attributes), parsed and stored in the local database. While getting and parsing data from API app displays like music data is refreshing from server. In this case when user tap home button and locks the device, After five minutes music has been stopped even the bundled songs are not finished. In the above case If I did not fetch data from API then music continuously playing.


Below code is used to play music.


guard let bundle = Bundle.main.path(forResource: "bird", ofType: "wav") else{return}


let alertSound = URL(fileURLWithPath: bundle)

try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.moviePlayback, policy: AVAudioSession.RouteSharingPolicy.default, options: AVAudioSession.CategoryOptions.mixWithOthers)

try AVAudioSession.sharedInstance().setActive(true)

try self.player = AVAudioPlayer(contentsOf: alertSound)

self.player?.prepareToPlay()

self.player?.play()


And URLSession is used for fetching data from API.


Please help me out to overcome this issue. Any suggestions to play music in the background while fetching and parsing data from API?


Thanks in advance

Replies

Did you find a solution. I'm having the same issue.