Nick
Nick

Reputation: 3394

How to play Vimeo content on tvOS

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?

Upvotes: 10

Views: 1606

Answers (3)

anas.p
anas.p

Reputation: 2286

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()
                }
            }
        }
    }
}

Upvotes: 2

xinatanil
xinatanil

Reputation: 634

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.

Upvotes: 3

Adam Johnson
Adam Johnson

Reputation: 2216

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

AVPlayer Documentation

Upvotes: -4

Related Questions