I'm trying to duplicate this https://jmacmullin.wordpress.com/2010/11/03/adding-meta-data-to-video-in-ios/ in swift.
Here is a video of Jake's code in action...
Objective C Timed Metadat in HLS stream
Here is an additional link to something similar...
http://cloudfields.net/blog/metadata-audiostream-mpmovieplayercontroller/
When my video is playing timed metadata should update a button to redirect to a specific youtube url in a webview when clicked. My video is roughly 15 min long and has 6 timed metadata urls.
I can't find any code or documentation anywhere on how to achieve this in Swift. I've managed to convert Jake's Objective C code for his Notification call.
// Register for meta-data notifications
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(metadataUpdate:)
name:MPMoviePlayerTimedMetadataUpdatedNotification
object:nil];
to Swift Code
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "metadataUpdated",
name: MPMoviePlayerTimedMetadataUpdatedNotification,
object: nil)
Jake's Function
Actor *actor = [[Actor alloc] init];
if ([player timedMetadata]!=nil && [[player timedMetadata] count] > 0) {
for (MPTimedMetadata *metadata in [player timedMetadata]) {
if ([[metadata.allMetadata valueForKey:@"key"] isEqualToString:@"TPE1"]) {
[actor setName:[metadata.allMetadata objectForKey:@"value"]];
}
if ([[metadata.allMetadata valueForKey:@"key"] isEqualToString:@"WXXX"]) {
NSURL *url = [NSURL URLWithString:[metadata.allMetadata objectForKey:@"value"]];
[actor setProfileURL:url];
}
}
}
// display some UI element for the user to interact with
to Swift
func metadataUpdated (notification: NSNotification!) {
if moviePlayer?.timedMetadata != nil && moviePlayer?.timedMetadata.count > 0 {
for MPTimedMetadata in [moviePlayer?.timedMetadata] {
if MPTimedMetadata?.description == ("TPE1") {
let name = ("value")
}
if MPTimedMetadata?.description == ("WXXX") {
var url = NSURL.observeValueForKeyPath("value")
}
}
}
println("Things are kind of working")
}
}
For the life of me though I cant figure out how to turn the metadata into any actionable code. I really need to create a button that redirects to the URL carried in the metadata, but can't convert it to a String. Any help is greatly appreciated. Here is what i have so far.
import UIKit
import MediaPlayer
class ViewController: UIViewController {
var moviePlayer: MPMoviePlayerController?
var youtube = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL(string: "http://path/to/video.m3u8")
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
player.view.sizeToFit()
player.scalingMode = MPMovieScalingMode.None
player.movieSourceType = MPMovieSourceType.Streaming
//player.repeatMode = MPMovieRepeatMode.One
player.play()
self.view.addSubview(player.view)
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "metadataUpdated:",
name: MPMoviePlayerTimedMetadataUpdatedNotification,
object: nil)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func metadataUpdated (notification: NSNotification!) {
if moviePlayer?.timedMetadata != nil && moviePlayer?.timedMetadata.count > 0 {
for MPTimedMetadata in [moviePlayer?.timedMetadata] {
if MPTimedMetadata?.description == ("TPE1") {
let name = ("value")
}
if MPTimedMetadata?.description == ("WXXX") {
var url = NSURL.observeValueForKeyPath("value")
}
}
}
println("Things are kind of working")
}
}