Search code examples
objective-cswiftwatchkitapple-watchwatchos

WatchKit: Video Duration


Is there a way to determine the duration of a video currently set to WKInterfaceInlineMovie? I need to implement a circular progress bar displaying a current progress.

I have a URL of the file initially downloaded from network. It plays well, but I haven't found any way to determine its length (actually, nor questions asking that which is strange).

Of course, I can ask the backend server to send this info, but I'd like to avoid such complications if possible.


Solution

  • OK, it seems I overcame the WKInterfaceInlineMovie API limitation by the help of AVFoundation and CoreMedia.

    I create AVAsset object using a movie URL from a shared folder (AVAsset(url:)). Then I get CMTime duration from the AVAsset's duration property (which is a CMTime object).

    Actually, I was very surprised to find out that it works. I'm still testing it, because it's too good to be true and I'm expecting to run across some pitfalls. I'll update the answer if anything else's found.