0

I am trying to load videos/playlists from YouTube (personal account) using Swift. I added the YTPlayerView class and iframe HTML file to do so but the videos are not loading.

I added a UIView in the storyboard and changed its class to YTPlayerView and created an IBOutlet.

Here's the code:

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

    }

}

As far as I read in the documentation this is what it takes to show a video. But when I run the app the UIView does nothing. How can I get this working?

JAL
  • 41,701
  • 23
  • 172
  • 300
  • The [YouTube iOS Helper](http://github.com/youtube/youtube-ios-player-helper/) (`YTPlayerView`) is different from XCDYouTubeKit. Are you sure you added the `YTPlayerView` class and imported it through your bridging header? – JAL Jun 29 '15 at 18:56
  • I just realized I mixed them in my question. I already changed the title. Thanks for checking this. Yes, I added #import “YTPlayerView.h” in my bridging header file. – Fernando Augusto Marins Jun 29 '15 at 18:59
  • Are you getting any errors in the debugger? There have been reported problems with Swift devs here: https://github.com/youtube/youtube-ios-player-helper/issues/107 – JAL Jun 29 '15 at 19:04
  • No, any errors. I actually fixed the one you are pointing out with nemoeslovo's pull request. – Fernando Augusto Marins Jun 29 '15 at 19:16
  • Do you get the same results when using `loadWithVideoId:` rather than `loadVideoByURL:`? – JAL Jun 29 '15 at 19:22
  • When I used loadWithVideoId it throws an error: 2015-06-29 16:28:12.308 RiciCloud 1.0[43385:2654461] Received error rendering template: Error Domain=NSCocoaErrorDomain Code=258 "The operation couldn’t be completed. (Cocoa error 258.)". Any thoughts? By the way, thanks for helping! – Fernando Augusto Marins Jun 29 '15 at 19:29
  • Did you add the `YTPlayerView-iframe-player.html` file to your project as well as the `YTPlayerView.h/m` files? – JAL Jun 29 '15 at 20:04
  • Yes I did, all files are present. – Fernando Augusto Marins Jun 29 '15 at 23:15
  • Try replacing `NSString *path = [[NSBundle mainBundle] pathForResource:@"YTPlayerView-iframe-player" ofType:@"html" inDirectory:@"Assets"];` with `NSString *path = [[NSBundle mainBundle] pathForResource:@"YTPlayerView-iframe-player" ofType:@"html"];` in `embedHTMLTemplate` instead of using the fix from the above PR. – JAL Jun 30 '15 at 00:19
  • I replaced the code as you suggested, but I still get the same error: 2015-06-29 23:38:35.644 RiciCloud 1.0[44585:2820372] Received error rendering template: Error Domain=NSCocoaErrorDomain Code=258 "The operation couldn’t be completed. (Cocoa error 258.)" – Fernando Augusto Marins Jun 30 '15 at 02:39
  • So that error means the player html file isn't being loaded properly from the bundle. Check if the path string is nil. If it is, the class is looking for the html file in the wrong place or the file is not being retrieved from the bundle properly. – JAL Jun 30 '15 at 13:33
  • Now it works! I added the html file in the copy bundle resources and now it works just fine! Can you please answer my question so I mark your answer as answer, please? Thank you so much!!!! – Fernando Augusto Marins Jun 30 '15 at 16:11
  • Answered, glad you figured it out! – JAL Jun 30 '15 at 16:12

4 Answers4

0

Make sure the YTPlayerView-iframe-player.html file has been added to the project and is being copied to the bundle under "Copy Bundle Resources."

JAL
  • 41,701
  • 23
  • 172
  • 300
  • @wil Have you tried replacing the code in this comment? http://stackoverflow.com/questions/31119798/how-to-proper-use-youtube-ios-player-helper/31143302?noredirect=1#comment50267473_31119798 – JAL Nov 05 '15 at 02:29
0

You have to delegate to view 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();
0

Make sure that YTPlayerView-iframe-player.html is added to your project as well as added to your bundle under "Copy Bundle Resources." also YTPlayerView.m this file also added to your project.

If still don't work than replace the following code

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

with this

NSString *path = [[NSBundle mainBundle] pathForResource:@"YTPlayerView-iframe-player" ofType:@"html"];
Ruchin Somal
  • 1,073
  • 12
  • 14
0

add self.view = videoPlayer in viewDidLoad:

@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

}

I hope this will work!

Timisorean
  • 1,388
  • 7
  • 20
  • 30