如何正确使用YouTube iOS Player Helper?

时间:2015-06-29 15:34:59

标签: ios swift youtube youtube-api ytplayerview

我正在尝试使用Swift从YouTube(个人帐户)加载视频/播放列表。我添加了YTPlayerView类和iframe HTML文件,但视频未加载。

我在故事板中添加了一个UIView,并将其类更改为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什么都不做。我怎样才能使这个工作?

4 个答案:

答案 0 :(得分:0)

确保YTPlayerView-iframe-player.html文件已添加到项目中,并且正在复制到"复制捆绑资源下的捆绑包。"

答案 1 :(得分:0)

您必须委托查看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();

答案 2 :(得分:0)

确保将YTPlayerView-iframe-player.html添加到您的项目以及“复制捆绑资源”下的捆绑中。还将YTPlayerView.m此文件也添加到您的项目中。

如果仍然无法运行,请替换以下代码

NSString *path = [[NSBundle mainBundle] pathForResource:@"YTPlayerView-iframe-player" ofType:@"html" inDirectory:@"Assets"];

与此

NSString *path = [[NSBundle mainBundle] pathForResource:@"YTPlayerView-iframe-player" ofType:@"html"];

答案 3 :(得分:0)

在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

}

我希望这会成功!