fix(ios): invalid metadata handling (#4422)

- When the metadata has the tag "iTunSMPB" or "iTunNORM" then the metadata is not converted correctly and comes [nil, nil, ...] which leads to crash

Co-authored-by: Alexander <aleksandar.todorov@serviceos.com>
This commit is contained in:
Alexander Todorov
2025-03-10 23:03:05 +02:00
committed by GitHub
parent 9055b14cf4
commit bc533e53b0

View File

@@ -208,10 +208,25 @@ class NowPlayingInfoCenterManager {
// commonMetadata is metadata from asset, externalMetadata is custom metadata set by user
// externalMetadata should override commonMetadata to allow override metadata from source
let metadata = {
let common = Dictionary(uniqueKeysWithValues: currentItem.asset.commonMetadata.map { ($0.identifier, $0) })
let external = Dictionary(uniqueKeysWithValues: currentItem.externalMetadata.map { ($0.identifier, $0) })
return Array((common.merging(external) { _, new in new }).values)
// When the metadata has the tag "iTunSMPB" or "iTunNORM" then the metadata is not converted correctly and comes [nil, nil, ...]
// This leads to a crash of the app
let metadata: [AVMetadataItem] = {
func processMetadataItems(_ items: [AVMetadataItem]) -> [String: AVMetadataItem] {
var result = [String: AVMetadataItem]()
for item in items {
if let id = item.identifier?.rawValue, !id.isEmpty, result[id] == nil {
result[id] = item
}
}
return result
}
let common = processMetadataItems(currentItem.asset.commonMetadata)
let external = processMetadataItems(currentItem.externalMetadata)
return Array(common.merging(external) { _, new in new }.values)
}()
let titleItem = AVMetadataItem.metadataItems(from: metadata, filteredByIdentifier: .commonIdentifierTitle).first?.stringValue ?? ""