Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HLS Metadata ID3 tag not working

I have a list of audio URLs in a TableView, so every time I tapped on each cell on didSelectRowAt this method will be called

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // Prepare Audio URL
    let audioUrl = URL(string: (channelSelected.audioUrl?.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed))!)
    let playerItem = AVPlayerItem(url: audioUrl!)
    playerItem.addObserver(self, forKeyPath: "timedMetadata", options: .new, context: nil)
    player = AVPlayer(playerItem: playerItem)
    playerViewController = AVPlayerViewController()
    playerViewController.player = player
    present(playerViewController, animated: true, completion: {
        self.playerViewController.player?.play()
    })
}

And based on the tutorials, I implemented observe value listener

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    print("keypath = \(keyPath!)")
    let avPlayerItem: AVPlayerItem = object as! AVPlayerItem
    if let timedMetadata = avPlayerItem.timedMetadata {
        print("Timed metadata = \(timedMetadata)")
    } else {
        print("Timed metadata nil")
    }
}

The problem is that timedMetadata is always nil. Help would be appreciated.

like image 1000
Lawrence Gimenez Avatar asked Apr 05 '18 02:04

Lawrence Gimenez


1 Answers

This is another alternative because I found out the server was not using "timed metadata". Here is how we implemented on our end, for those who haven't found an answer. The backend is using Wowza Server.

let playerItem = AVPlayerItem(url: audioUrl!)
let adID = AVMetadataItem.identifier(forKey: "X-TITLE", keySpace: .hlsDateRange)
let metadataCollector = AVPlayerItemMetadataCollector(identifiers: [adID!.rawValue], classifyingLabels: nil)
        metadataCollector.setDelegate(self, queue: DispatchQueue.main)
playerItem.add(metadataCollector)

and then declare an extension of AVPlayerItemMetadataCollectorPushDelegate

    func metadataCollector(_ metadataCollector: AVPlayerItemMetadataCollector, didCollect metadataGroups: [AVDateRangeMetadataGroup], indexesOfNewGroups: IndexSet, indexesOfModifiedGroups: IndexSet) {
             for metadataGroup in metadataGroups {
                     for metadata in metadataGroup.items {
             }
                     }
    }
like image 102
Lawrence Gimenez Avatar answered Oct 18 '22 11:10

Lawrence Gimenez