timedMetaData value has wrong encoding

390 views Asked by At

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?

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

            print(value)
            //metadata = item.stringValue!
        } else {
            print(item.key)
        }
    }
}
1

There are 1 answers

2
Tommy Ovesen On BEST ANSWER

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);