10

Similar to this question asking about how to play YouTube videos on tvOS, I'd like to play Vimeo videos in the app I'm building. However, as explained here, regular web views (which is how I do things in iOS) are out.

How would I go about playing a Vimeo video on tvOS assuming I knew the URL to the video page but not the URL to the raw .mp4 file?

Community
  • 1
  • 1
Nick
  • 3,172
  • 3
  • 37
  • 49
  • PS: I'm looking for something that doesn't violate the Vimeo Terms of Service, but I'd also be interested in hearing about libraries like [HCYouTubeParser](https://github.com/hellozimi/HCYoutubeParser) that can figure out the .mp4 URL. – Nick Sep 16 '15 at 17:24
  • I imagine we'll have to wait until Vimeo updates their API to do something like this on tvOS but thought I'd get the question out there anyway. – Nick Oct 06 '15 at 18:57

3 Answers3

3

Well, apparently, there is no way to play a Vimeo video in a WebView, because tvOS SDK doesn't have a WebView. The only option we have is AVPlayer, but it requires a direct URL to a video, and Vimeo won't give us direct video URLs for free. Right now, the only possible way is to purchase Vimeo's PRO membership ($199 per year at the moment) and retrieve direct video URLs via Vimeo's API.

EDIT: as nickv2002 noted, this approach will give you direct URLs only to YOUR OWN videos. Thi means, that even with Vimeo PRO you can't just take any video on Vimeo and get a direct URL for it.

xinatanil
  • 1,085
  • 2
  • 13
  • 23
  • 1
    Thanks for your answer. A Vimeo Pro membership would be a good option if you just wanted to use the service to distribute your own videos. Unfortunately this is unfeasible to use if you want to watch *other* users videos on tvOS. – Nick Oct 06 '15 at 18:55
2

Using this pod, tvOS can play vimeo contents, Install

pod 'YTVimeoExtractor'

and

 import YTVimeoExtractor

And use this function to play the video

func playVimeoVideo(videoId: String)
{
    YTVimeoExtractor.shared().fetchVideo(withVimeoURL: "https://vimeo.com/video/\(videoId)", withReferer: nil) { (video:YTVimeoVideo?, error:Error?) in

        if let streamUrls = video?.streamURLs
        {
            var streamURL: String?
            var streams : [String:String] = [:]
            for (key,value) in streamUrls {

                streams["\(key)"] = "\(value)"
                print("\(key) || \(value)")
            }

            if let large = streams["720"]
            {
                streamURL = large
            }
            else if let high = streams["480"]
            {
                streamURL = high
            }
            else if let medium = streams["360"]
            {
                streamURL = medium
            }
            else if let low = streams["270"]
            {
                streamURL = low
            }

            if let url = streamURL
            {
                let videoURL = NSURL(string: url)
                let player = AVPlayer(url: videoURL! as URL)
                let playerViewController = AVPlayerViewController()
                playerViewController.player = player
                self.present(playerViewController, animated: true) {
                    playerViewController.player!.play()
                }
            }
        }
    }
}
anas.p
  • 2,246
  • 19
  • 26
  • On to other projects these days, so I haven't tested, but this seems like a good solution. Thanks, +1 – Nick Feb 11 '17 at 00:20
-4

You can play your video using AVPlayer, just like on iOS.

AVPlayer Documentation

Adam Johnson
  • 2,198
  • 1
  • 17
  • 23
  • 2
    Sure, if I had the full URL to the .mp4 file (which I can't without violating the Terms of Service as I explained in my question). But I don't think I can give AVPlayer a URL like https://vimeo.com/2619976 and expect it to play. Or am I missing some magic AVPlayer functionality? – Nick Sep 21 '15 at 17:48