Skip to content

Commit fc3cde5

Browse files
authored
Merge pull request #40 from nicolassrod/master
Enhance metadata validators
2 parents b47cca8 + 71848c2 commit fc3cde5

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

Sources/FRadioPlayer/FRadioAPI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal struct FRadioAPI {
6262
// Strip off trailing '[???]' characters left there by ShoutCast and Centova Streams
6363
// It will leave the string alone if the pattern is not there
6464

65-
let pattern = "(\\[.*?\\]\\w*$)"
65+
let pattern = #"(\(.*?\)\w*)|(\[.*?\]\w*)"#
6666
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { return rawValue }
6767

6868
let rawCleaned = NSMutableString(string: rawValue)

Sources/FRadioPlayer/FRadioPlayer.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ open class FRadioPlayer: NSObject {
399399
}
400400

401401
private func timedMetadataDidChange(rawValue: String?) {
402-
let parts = rawValue?.components(separatedBy: " - ")
402+
let metadataCleaned = cleanMetadata(rawValue)
403+
let parts = metadataCleaned?.components(separatedBy: " - ")
403404
delegate?.radioPlayer?(self, metadataDidChange: parts?.first, trackName: parts?.last)
404405
delegate?.radioPlayer?(self, metadataDidChange: rawValue)
405406
shouldGetArtwork(for: rawValue, enableArtwork)
@@ -418,7 +419,16 @@ open class FRadioPlayer: NSObject {
418419
}
419420
})
420421
}
421-
422+
423+
private func cleanMetadata(_ rawValue: String?) -> String? {
424+
guard let rawValue = rawValue else { return nil }
425+
return rawValue.replacingOccurrences(
426+
of: #"(\(.*?\)\w*)|(\[.*?\]\w*)"#,
427+
with: "",
428+
options: .regularExpression
429+
)
430+
}
431+
422432
private func reloadItem() {
423433
player?.replaceCurrentItem(with: nil)
424434
player?.replaceCurrentItem(with: playerItem)

0 commit comments

Comments
 (0)