That is my first question and I did not find any answer about this issue. I have a JSON with more than 2 thousand tracks on my server. And when I make an array of AVPlayerItem to insert into the AVQueuePlayer, It takes a lot of time. I want to load my tracks gradually. Here is my code:
class Player: ObservableObject {
// Root link of my tracks
private let link: String = "http://audio.radiohustle.online/"
// Link with my tracks on the server
private let requestLink: String = "http://data.radiohustle.online/player/list/"
// AVQueuePlayer instance
var queuePlayer: AVQueuePlayer?
var playerItems: [AVPlayerItem] = []
// Function to make an array of AVPlayerItems and starting a Playback
func makePlaylist() { to function
AF.request(self.requestLink, method: .get).validate().responseDecodable(of: [Song].self) { response in
switch response.result {
case .success(let value):
self.data = value.sorted(by: {$0.bpm < $1.bpm})
self.test()
case .failure(let error):
print(error)
}
}
}
// Here is the Question
func test() {
guard let data = self.data else { return }
// Iteration and inserting all tracks from JSON to [AVPlayerItem]. It takes a lot of time to process 2000+ tracks
for song in data {
guard let songURL = URL(string: self.link + song.src) else {
print("No songs")
continue
}
self.playerItems.append(AVPlayerItem(url: songURL))
}
guard let playerItems = playerItems else { return }
self.queuePlayer = AVQueuePlayer(items: self.playerItems)
self.queuePlayer?.play()
}
}
How to load tracks gradually, like in Apple Music App for example?
I was looking for information: Medium Tutorial with 4 parts, Youtube videos, tried to separate my array of tracks separating by smaller blocks. But it does not seem as a good approach.