如何正确使用 YouTube iOS Player Helper?
How to proper use YouTube iOS Player Helper?
我正在尝试使用 Swift 从 YouTube(个人帐户)加载 videos/playlists。为此,我添加了 YTPlayerView
class 和 iframe HTML 文件,但视频未加载。
我在情节提要中添加了一个 UIView 并进行了更改它 class 到 YTPlayerView 并创建了一个 IBOutlet。
代码如下:
import UIKit
import youtube_ios_player_helper
class YTViewController: UIViewController, YTPlayerViewDelegate {
@IBOutlet var videoPlayer: YTPlayerView!
override func viewDidLoad() {
let videoID = "https://www.youtube.com/watch?v=M7lc1UVf-VE"
videoPlayer.loadVideoByURL(videoID, startSeconds: 0.0, suggestedQuality: .Small)
videoPlayer.playVideo()
}
}
据我在文档中所读,这就是显示视频所需要的。但是当我 运行 应用程序时,UIView 什么都不做。我怎样才能让它工作?
确保 YTPlayerView-iframe-player.html
文件已添加到项目中并正在复制到 "Copy Bundle Resources."
下的包中
您必须委托才能查看videoPlayer.delegate = self;
let videoID = "https://www.youtube.com/watch?v=M7lc1UVf-VE"; videoPlayer.delegate = self;
videoPlayer.loadVideoByURL(videoID, startSeconds: 0.0, suggestedQuality: .Small);
videoPlayer.playVideo();
确保 YTPlayerView-iframe-player.html 已添加到您的项目并添加到 "Copy Bundle Resources." 下的包中 YTPlayerView.m 此文件也已添加到您的项目.
如果还是不行更换下面的代码
NSString *path = [[NSBundle mainBundle] pathForResource:@"YTPlayerView-iframe-player" ofType:@"html" inDirectory:@"Assets"];
有了这个
NSString *path = [[NSBundle mainBundle] pathForResource:@"YTPlayerView-iframe-player" ofType:@"html"];
在viewDidLoad中添加self.view = videoPlayer
:
@IBOutlet var videoPlayer: YTPlayerView!
override func viewDidLoad() {
let videoID = "https://www.youtube.com/watch?v=M7lc1UVf-VE"
videoPlayer.loadVideoByURL(videoID, startSeconds: 0.0, suggestedQuality: .Small)
videoPlayer.playVideo()
videoPlayer.delegate = self
self.view = videoPlayer
}
我希望这会奏效!
我正在尝试使用 Swift 从 YouTube(个人帐户)加载 videos/playlists。为此,我添加了 YTPlayerView
class 和 iframe HTML 文件,但视频未加载。
我在情节提要中添加了一个 UIView 并进行了更改它 class 到 YTPlayerView 并创建了一个 IBOutlet。
代码如下:
import UIKit
import youtube_ios_player_helper
class YTViewController: UIViewController, YTPlayerViewDelegate {
@IBOutlet var videoPlayer: YTPlayerView!
override func viewDidLoad() {
let videoID = "https://www.youtube.com/watch?v=M7lc1UVf-VE"
videoPlayer.loadVideoByURL(videoID, startSeconds: 0.0, suggestedQuality: .Small)
videoPlayer.playVideo()
}
}
据我在文档中所读,这就是显示视频所需要的。但是当我 运行 应用程序时,UIView 什么都不做。我怎样才能让它工作?
确保 YTPlayerView-iframe-player.html
文件已添加到项目中并正在复制到 "Copy Bundle Resources."
您必须委托才能查看videoPlayer.delegate = self;
let videoID = "https://www.youtube.com/watch?v=M7lc1UVf-VE"; videoPlayer.delegate = self;
videoPlayer.loadVideoByURL(videoID, startSeconds: 0.0, suggestedQuality: .Small);
videoPlayer.playVideo();
确保 YTPlayerView-iframe-player.html 已添加到您的项目并添加到 "Copy Bundle Resources." 下的包中 YTPlayerView.m 此文件也已添加到您的项目.
如果还是不行更换下面的代码
NSString *path = [[NSBundle mainBundle] pathForResource:@"YTPlayerView-iframe-player" ofType:@"html" inDirectory:@"Assets"];
有了这个
NSString *path = [[NSBundle mainBundle] pathForResource:@"YTPlayerView-iframe-player" ofType:@"html"];
在viewDidLoad中添加self.view = videoPlayer
:
@IBOutlet var videoPlayer: YTPlayerView!
override func viewDidLoad() {
let videoID = "https://www.youtube.com/watch?v=M7lc1UVf-VE"
videoPlayer.loadVideoByURL(videoID, startSeconds: 0.0, suggestedQuality: .Small)
videoPlayer.playVideo()
videoPlayer.delegate = self
self.view = videoPlayer
}
我希望这会奏效!