P.Coder P.Coder - 5 months ago 47
Swift Question

Extract video URL from NSNotification Asset

I have a small question for big programmers just like you:)

I'm building IOS application using Swift 2 and I have a video player which works fine, I just want to get the current video url using nsnotification as following.

in viewDidLoad()

// add notification
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MZAvailableDownloadsViewController.videoHasStarted(_:)), name: "AVPlayerItemBecameCurrentNotification", object: nil)
}

func videoHasStarted(notification: NSNotification) {
print(notification.object?.asset)
// HOW TO EXTRACT VIDEO URL?
}


The output is:

Optional(<AVURLAsset: 0x7faa81c02e20, URL = http://www.emanway.com/media/2016/06/22/17_Elahi.mp4>)


as you can see, how can I get the video url from the output? Thanks in advance

Answer

AVURLAsset has an URL property.

To use it, cast your object to AVURLAsset instead of AnyObject.

Something like this should work:

if let asset = notification.object?.asset as? AVURLAsset {
    let videoURL = asset.URL

}
Comments