在 iOS 应用中播放 Youtube 视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44499332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:13:48  来源:igfitidea点击:

To play Youtube Video in iOS app

iosswiftswift3

提问by Prathamesh

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

我想在我的 iOS 应用中播放 YouTube 视频。我进行了搜索,但我找到的唯一解决方案是将 youtube 视频嵌入到 iOS 应用程序中,其中视频在 webview 中播放,因此,我们可以滚动并播放其他建议的视频。我不想在 webview 中播放视频,我想像在播放器中播放一样播放视频,而用户无法滚动它。在 Swift 中是否有任何解决方案,而且我不想使用违反 Youtube 条款和条件的库

回答by Adam

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

如果您不想使用 YouTube 提供的 API 而是继续使用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>.

YouTube 具有在 webview 中以全屏方式加载任何视频的功能,而无需使用格式中的 URL 的任何滚动功能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.

例如,要使用此方法加载江南 Style,只需将 UIWebView 定向到 URL https://www.youtube.com/embed/9bZkp7q19f0 即可

回答by Adam

The API that YouTube provides to embed videos in iOS apps is indeed written in Objective-C, but it works just as well in Swift.

YouTube 提供的用于在 iOS 应用程序中嵌入视频的 API 确实是用 Objective-C 编写的,但它在 Swift 中也能正常工作。

To install the library via CocoaPods, follow the CocoaPods setup instructionsand add the following line to your Podfile:

要通过 CocoaPods 安装库,请按照 CocoaPods 安装说明并将以下行添加到您的 Podfile 中:

pod ‘youtube-ios-player-helper', ‘~> 0.1'

Once you have run pod install, be sure to use the .xcworkspacefile from now on in Xcode.

运行后pod install,请确保.xcworkspace从现在开始在 Xcode 中使用该文件。

To import the pod, simply use the following import statement at the top of your Swift files:

要导入 pod,只需在 Swift 文件的顶部使用以下导入语句:

import youtube_ios_player_helper

You can then create youtube player views as follows:

然后,您可以按如下方式创建 youtube 播放器视图:

let playerView = YTPlayerView()

You can include this view in your layouts as you would any other UIView. In addition, it includes all of the functions listed in the YouTube documentation. For instance, to load and play a video, use the following function:

您可以像对待任何其他 UIView 一样在布局中包含此视图。此外,它还包括 YouTube 文档中列出的所有功能。例如,要加载和播放视频,请使用以下函数:

playerView.load(withVideoId: videoId);

Where videoIdis the string id found in the URL of the video, such as "9bZkp7q19f0".

videoId视频网址中找到的字符串id在哪里,比如"9bZkp7q19f0".

回答by Mahesh Chaudhari

Play youtube video in Swift 4.0

在 Swift 4.0 中播放 YouTube 视频

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

回答by Rosalie W

There also are solutions for playing Youtube Videos in an app on Github.

也有在 Github 上的应用程序中播放 Youtube 视频的解决方案。

Like this one: https://github.com/rinov/YoutubeKit

像这个:https: //github.com/rinov/YoutubeKit

Or this one: https://github.com/gilesvangruisen/Swift-YouTube-Player

或者这个:https: //github.com/gilesvanruisen/Swift-YouTube-Player

Just simply add the pod for the project that you want to use, install the pod in terminal, and you can use the functionality in that project.

只需简单地为您要使用的项目添加 pod,在终端中安装 pod,您就可以使用该项目中的功能。

Hope that this is helpful.

希望这是有帮助的。