InlineYoutubeView 0.4.0

InlineYoutubeView 0.4.0

Shubhankar Yash维护。



  • shubhankaryash

ios-inline-youtube-view

Version Build Status License Platform

适用于安卓、iOS和React的YouTube组件。这是一套在您的安卓iOS或React Native应用中使用YouTube的实用库。

youtube-ios

这个Pod是对YouTube提供的youtube-ios-helper的修改。修改包括

  • 迁移到WkWebView从旧的UIWebView。WKWebView在您的应用之外单独运行,以便可以对本地Safari JavaScript进行优化。这意味着WKWebView加载网页比UIWebView更快、更高效,并且不会产生太多内存开销。引用Apple文档 - "从iOS 8.0和OS X 10.10开始,使用WKWebView将网页内容添加到您的应用中。不要使用UIWebView或WebView。"
  • 添加对自定义html url的支持。以前我们只能在资源包中使用html
  • 添加参数以决定是否在页面内嵌或全屏播放视频。
  • 添加在加载iframeAPI后网络离线时的错误回调。

演示应用

要运行示例项目,请先克隆仓库,然后从示例目录运行pod install

演示GIF

YouTube iOS

安装

inlineYouTubeView 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod 'InlineYoutubeView'

用法

导入头文件

ObjectiveC

#import <InlineYoutubeView/InlineYoutubeView.h>

Swift

import InlineYoutubeView

创建 InlineYoutubeView 对象。

ObjectiveC

@property (nonatomic, strong) InlineYoutubeView *youtubeView;

Swift

var youtubeView: InlineYoutubeView

初始化 Inline youtube 视图。

ObjectiveC

//The url where the HTML is hosted. You can have any custom HTML url as well. So you can modify the iframe provided, upload the modified HTML file and use the url here
NSString *const HTML_URL = @"https://cdn.rawgit.com/flipkart-incubator/inline-youtube-view/60bae1a1/youtube-android/youtube_iframe_player.html";

//Incase you need your youtube view to open inline
self.youtubeView = [[InlineYoutubeView alloc] initWithHtmlUrl:HTML_URL andVideoPlayerMode:kYTPlayerModeInline];

//Incase you need your youtube view to open in fullscreen
self.youtubeView = [[InlineYoutubeView alloc] initWithHtmlUrl:HTML_URL andVideoPlayerMode:kYTPlayerModeFullScreen];

Swift

//The url where the HTML is hosted. You can have any custom HTML url as well. So you can modify the iframe provided, upload the modified HTML file and use the url here
let HTML_URL = "https://cdn.rawgit.com/flipkart-incubator/inline-youtube-view/60bae1a1/youtube-android/youtube_iframe_player.html"

//Incase you need your youtube view to open inline
youtubeView = InlineYoutubeView(htmlUrl: HTML_URL, andVideoPlayerMode: .inline)

//Incase you need your youtube view to open in fullscreen
youtubeView = InlineYoutubeView(htmlUrl: HTML_URL, andVideoPlayerMode: .fullScreen)

将youtube视图的代理设置为自己。这将确保您开始接收所有 InlineYoutubeView 回调。

ObjectiveC

self.youtubeView.delegate = self;

Swift

 youtubeView.delegate = self

加载iframe。如果它当前未加载,则 InlineYoutubeView 将在加载完毕时发出 playerViewDidBecomeReady 回调。如果已经加载,我们将立即调用此方法以启动视频。

ObjectiveC

//Wait for youtube player to to get ready or proceed if it is ready.
if([self.youtubeView loadYTIframe]) {
[self playerViewDidBecomeReady:self.youtubeView];
}

Swift

//Wait for youtube player to to get ready or proceed if it is ready.
if (ytPlayerView.loadYTIframe()) {
  playerViewDidBecomeReady(ytPlayerView)
}

实现 InlineYoutubeViewDelegate 的 playerViewDidBecomeReady 方法。当您的播放器准备就绪时,应调用此方法。

ObjectiveC

- (void)playerViewDidBecomeReady:(nonnull InlineYoutubeView *)playerView {
//Load the youtube video with the videoId of the video
[playerView loadVideoById:_videoId startSeconds:0 suggestedQuality:kYTPlaybackQualityAuto];
[playerView playVideo];
}

Swift

//Wait for youtube player to to get ready or proceed if it is ready.
func playerViewDidBecomeReady(_ playerView: InlineYoutubeView) {
    //Load the youtube video with the videoId of the video
    playerView.loadVideo(byId: videoId, startSeconds: 0, suggestedQuality: YTPlaybackQuality.auto)
    playerView.playVideo()
}

根据您的需求实现 InlineYoutubeViewDelegate 的其他方法。有关相同主题的更多文档,请参阅 InlineYoutubeView.h 文件。

- (void)playerView:(nonnull InlineYoutubeView *)playerView didChangeToState:(YTPlayerState)state;

- (void)playerView:(nonnull InlineYoutubeView *)playerView didChangeToQuality:(YTPlaybackQuality)quality;

- (void)playerView:(nonnull InlineYoutubeView *)playerView receivedError:(YTPlayerError)error ;

- (void)playerView:(nonnull InlineYoutubeView *)playerView didPlayTime:(float)playTime ;

- (void)playerView:(nonnull InlineYoutubeView *)playerView duration:(NSTimeInterval)duration ;

- (nonnull UIColor *)playerViewPreferredWebViewBackgroundColor:(nonnull InlineYoutubeView *)playerView;

- (nullable UIView *)playerViewPreferredInitialLoadingView:(nonnull InlineYoutubeView *)playerView;

- (void)playerViewDidEnterFullScreen:(nonnull InlineYoutubeView *)playerView;

- (void)playerViewDidExitFullScreen:(nonnull InlineYoutubeView *)playerView;

YouTube Android 播放器

我们也将 inline-youtube-player 对开源给 Android

贡献

最简单的方式是通过 Fork 仓库、付诸实施您的更改以及 创建 Pull Request 来贡献。

作者

shubhankaryash,[email protected]

许可

InlineYoutubeView 在 Apache 2.0 许可下可用。pod 文件是对谷歌工作的修改,有相应的许可。但是示例项目属于 Flipkart 许可。更多信息请参阅 LICENSE 文件。