I use AVQueuePlayer in my app and I need to have an array with my tracks with type [AVPlayerItem]. Array must creates every time when TableView loads. Also I have an array "content" with elements in current folder to create a table view cells. How can I add these elements to array "playerQueue"?
class TableViewController: UITableViewController {
var currentDirectory: String?
var content: [String] = []
var player: AVQueuePlayer!
var playerQueue: [AVPlayerItem] = []
override func viewDidLoad() {
super.viewDidLoad()
reloadData()
}
func reloadData(needsReload: Bool = true) {
if currentDirectory == nil {
currentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
}
do {
content = try FileManager.default.contentsOfDirectory(atPath: currentDirectory! )
} catch {
content = []
}
if needsReload {
tableView.reloadData()
}
}
If you have for example .m4v
files in your documents folder you should find all of them then convert your file paths to AVPlayerItem ones (URL -> AVURLAsset -> AVPlayerItem) e.g.:
if let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
if let content = try? FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil) {
let items = content
.filter { $0.pathExtension == "m4v" }
.map { AVURLAsset(url: $0)}
.map { AVPlayerItem(asset: $0) }
player = AVQueuePlayer(items: items)
player?.play()
}
}