When playing a live stream using the HTTP Live Streaming method, is it possible read the current metadata (eg. Title and Artist)? This is for an iPhone radio app.
Using the same protocol that powers the web, HLS deploys content using ordinary web servers and content delivery networks. HLS is designed for reliability and dynamically adapts to network conditions by optimizing playback for the available speed of wired and wireless connections.
Metadata in streaming media gives subscribers the opportunity to get information about the media they are viewing. Metadata can contain information about the video, such as title, copyright information, duration of the video, or creation date.
What is Timed Metadata? Timed metadata is metadata with timestamps. It can be inserted into a stream programmatically, using the Amazon IVS API. When Amazon IVS processes a stream, the timed metadata is synchronized with the audio and video frames.
Swift solution. This is a sample of simple streaming audio player. You can read metadata in the method of delegate AVPlayerItemMetadataOutputPushDelegate.
import UIKit
import AVFoundation
class PlayerViewController: UIViewController {
var player = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
configurePlayer()
player.play()
}
private func configurePlayer() {
guard let url = URL(string: "Your stream URL") else { return }
let asset = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
let metadataOutput = AVPlayerItemMetadataOutput(identifiers: nil)
metadataOutput.setDelegate(self, queue: DispatchQueue.main)
playerItem.add(metadataOutput)
player = AVPlayer(playerItem: playerItem)
}
}
extension PlayerViewController: AVPlayerItemMetadataOutputPushDelegate {
func metadataOutput(_ output: AVPlayerItemMetadataOutput, didOutputTimedMetadataGroups groups: [AVTimedMetadataGroup], from track: AVPlayerItemTrack?) {
let item = groups.first?.items.first
item?.value(forKeyPath: "value")
print(item!.value(forKeyPath: "value")!)
}
}
in swift 2.0 getting metadata info music streaming:
PlayerItem.addObserver(self, forKeyPath: "timedMetadata", options: NSKeyValueObservingOptions.New, context: nil)
add this method:
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
//Atualiza Nome Musica
if keyPath == "timedMetadata" {
if let meta = PlayerItem.timedMetadata {
print("Novo Metadata \(meta)")
for metadata in meta {
if let nomemusica = metadata.valueForKey("value") as? String{
LB_NomeMusica.text = nomemusica
if NSClassFromString("MPNowPlayingInfoCenter") != nil {
let image:UIImage = UIImage(named: "logo.gif")!
let albumArt = MPMediaItemArtwork(image: image)
var songInfo: [String:AnyObject] = [
MPMediaItemPropertyTitle: nomemusica,
MPMediaItemPropertyArtist: "Ao Vivo",
MPMediaItemPropertyArtwork: albumArt
]
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo
}
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With