Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To play Youtube Video in iOS app

Tags:

ios

swift

swift3

I want to play youtube videos in my iOS App. I searched for that but the only solution I found is to embed youtube videos in the iOS app, in which video plays in webview, so in that, we can scroll and also play other videos which are in suggestion. I don't want to play video in webview, I want to play video just like it plays in player and user cannot scroll it. Is there any solution for that in Swift and also I don't want to use libraries which are against terms and condition of Youtube

like image 543
Prathamesh Avatar asked Jun 12 '17 12:06

Prathamesh


People also ask

Why can't I play YouTube videos on my iPhone?

YouTube appTurn off your mobile data connection and then turn it on again. Uninstall and reinstall the YouTube app. Update to the newest available version of the YouTube app. Update to the newest available version of iOS.


2 Answers

Here's another solution if you don't want to use the API provided by YouTube and instead continue using a UIWebView.

YouTube has functionality to load any video in fullscreen in a webview without any of the scrolling features using a URL in the format https://www.youtube.com/embed/<videoId>.

For example, to load Gangnam Style using this method, simply direct the UIWebView to the URL https://www.youtube.com/embed/9bZkp7q19f0.

like image 199
Adam Avatar answered Nov 15 '22 19:11

Adam


Play youtube video in Swift 4.0

if let range = strUrl.range(of: "=") {
        let strIdentifier = strUrl.substring(from: range.upperBound)        
        let playerViewController = AVPlayerViewController()
        self.present(playerViewController, animated: true, completion: nil)
        XCDYouTubeClient.default().getVideoWithIdentifier(strIdentifier) { 

            [weak playerViewController] (video: XCDYouTubeVideo?, error: Error?) in
            if let streamURLs = video?.streamURLs, let streamURL = 
                (streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?? 
                streamURLs[YouTubeVideoQuality.hd720] ?? 
                streamURLs[YouTubeVideoQuality.medium360] ?? 
                streamURLs[YouTubeVideoQuality.small240]) {
                    playerViewController?.player = AVPlayer(url: streamURL)
                } else {
                    self.dismiss(animated: true, completion: nil)
                }
        }
}
like image 24
Mahesh Chaudhari Avatar answered Nov 15 '22 17:11

Mahesh Chaudhari