P.Coder P.Coder - 1 year ago 187
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) {

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 Source

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