tomen tomen - 2 years ago 86
Swift Question

timedMetaData value has wrong encoding

I'm developing a radio app for iOS and need to be able to parse metadata from an audio stream with an encoding that supports Norwegian characters "ÆØÅ".

Using timedMetaData on an AVPlayerItem these three characters are displayed as "ÃÃÃ".

Does anyone know how to change the encoding on the value (not stringValue) of a timedMetaData item? Or perhaps how to get the raw value?

I'm using Xcode 7 beta 5 (Swift 2.0).

Code example:

private var url = ""
private var player: AVPlayer!
private var playerItem: AVPlayerItem!

playerItem = AVPlayerItem(URL: NSURL(string: url)!)
player = AVPlayer(playerItem: playerItem)
playerItem.addObserver(self, forKeyPath: "timedMetadata", options: NSKeyValueObservingOptions.New, context: nil)

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

guard keyPath == "timedMetadata" else { return }

for item in playerItem.timedMetadata! {

if item.commonKey == "title" {

let value = item.value

//metadata = item.stringValue!
} else {

Answer Source

I found the solution using C# / Xamarin. You need to encode the metadata with ISOLatin1, then to UTF8:

NSString str = new NSString(item.StringValue);
var data = str.Encode(NSStringEncoding.ISOLatin1, true);
string meta = new NSString(data, NSStringEncoding.UTF8);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download