Reputation: 1
I am developing a live music streaming application for iOS. What I am trying to do is, extracting the metadata values of the music that is currently playing. Metadata is stored in the URL. Write now I am able to extract this as metaData.
Optional([<AVMetadataItem: 0x600001c69860, identifier=common/title, keySpace=comn, key class = __NSCFConstantString, key=title,
commonKey=title, extendedLanguageTag=(null), dataType=(null),
time={19392/22050 = 0.879}, duration={INVALID}, startDate=(null),
extras={ }, value class=__NSCFString, value=Electricity - Silk City &
Dua Lipa f./Diplo - 03:39 >])
The problem is there is only one common key ie "title" in the metadata item. This common key has a value that consist of song title, artist and song duration. How do I extract only the artist value from this common key..
This is how am I extracting metadata.
I added an observer to the AVPlayerItem for key path "timedMetadata"
playerItem.addObserver(self, forKeyPath: "timedMetadata", options: [], context: nil)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath != "timedMetadata" { return }
var data: AVPlayerItem = object as! AVPlayerItem
for item in data.timedMetadata as! [AVMetadataItem] {
if let stringValue = item.value as? String {
//print(item.commonKey)
if item.commonKey!.rawValue == "title" {
print(stringValue.description)
}
}
}
}
Getting this as value : Electricity - Silk City & Dua Lipa f./Diplo - 03:39
This value has the song title, song artists, and song duration. What I want is to get only the song artist as the value. metadata changes it value for each song that is currently being played.
Upvotes: 0
Views: 960
Reputation: 1
Finally have found a way around to solve this problem..
The value i was getting was separated by a common character (-). What I did is converted this single string into array of strings.
let stringValueArray = stringValue.components(separatedBy: "-")
let title = stringValueArray[0]
let author = stringValueArray[1]
let duration = stringValueArray[2]
I know this might not be the right approach, but still it works..
Upvotes: -1