youtube / youtube-ios-player-helper

Lightweight helper library that allows iOS developers to add inline playback of YouTube videos through a WebView
Other
1.64k stars 681 forks source link

Embedding YTPlayerView In UIScrollView #132

Open cat-aged-noodle-prius opened 9 years ago

cat-aged-noodle-prius commented 9 years ago

I'm creating a UIScrollView that can hold images and videos from different sources. I first created and added a UIView for each image/video to the UIScrollView, then add the image/video view/layer. This works fine for images and videos using AVFoundation; however, does not work for Youtube videos.

Here's my code for adding the YTPlayerView to the UIView within the UIScrollView:

YTPlayerView * youtubePlayerView = [[YTPlayerView alloc] initWithFrame: CGRectMake(0, 0, self.scrollViewWidth, self.scrollViewHeight)];
youtubePlayerView.bounds = CGRectMake(0, 0, self.scrollViewWidth, self.scrollViewHeight);

NSString * youtubePath = [media getMediaPath]; //Valid Youtube URL as NSString

NSMutableString *videoUrlCopy = [NSMutableString stringWithString:youtubePath];

NSString *vID =  [videoUrlCopy lastPathComponent];

NSDictionary *playerVars = @{
                            @"playsinline" : @1
                            };

youtubePlayerView.delegate = self;

[contentView addSubview:youtubePlayerView]; //ContentView is subview of UIScrollview

[youtubePlayerView loadWithVideoId:vID playerVars:playerVars];
[youtubePlayerView playVideo];

When I try to load the youtube video with loadWithVideoId, the process terminates within YTPlayerView here: UIWebView *webView = [[UIWebView alloc] initWithFrame:self.bounds]; throwing EXC_BAD_ACCESS.

- (UIWebView *)createNewWebView {
  UIWebView *webView = [[UIWebView alloc] initWithFrame:self.bounds];
  webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
  webView.scrollView.scrollEnabled = NO;
  webView.scrollView.bounces = NO;
  return webView;
}

Interestingly, I've gotten the Youtube videos to show adding the YTPlayerView directly as a subview to self.view. Any suggestions?

tivenlou commented 9 years ago

here is my swift code . my help some.

var img_arr = ["img01.jpeg","img02.jpeg","img03.jpeg",""]

override func viewDidLoad() {
    super.viewDidLoad()

    var _scrollview = UIScrollView()
    //set up scrollview
    _scrollview.frame = CGRectMake(0, 0, view.frame.width, view.frame.width/16*9)
    _scrollview.backgroundColor = UIColor.whiteColor()
    _scrollview.delegate = self
    _scrollview.pagingEnabled = true
    _scrollview.showsHorizontalScrollIndicator = false
    view.addSubview(_scrollview)

    var page_counts = UIPageControl()
    //add show page counts
    page_counts.frame = CGRectMake(0, view.frame.height-25, view.frame.width, 20)
    page_counts.backgroundColor = UIColor.clearColor()
    page_counts.pageIndicatorTintColor = UIColor.whiteColor()
    page_counts.currentPageIndicatorTintColor = UIColor.orangeColor()
    page_counts.alpha = 0.9
    //        page_counts.currentPage = 0
    page_counts.hidesForSinglePage = true
    page_counts.defersCurrentPageDisplay = true
    view.addSubview(page_counts)

    var frame: CGRect = CGRectMake(0, 0, 0, 0)

    for var p=0;p<4;p++ {

        frame.origin.x = _scrollview.frame.size.width * CGFloat(p)
        frame.size = _scrollview.frame.size

        //add ytplayer
        if p == 3 {

            var playerVars:NSDictionary = ["playsinline": "0","rel":"0"]

            var videoId = "M7lc1UVf-VE"
            var youtubeplayer = YTPlayerView()

// youtubeplayer.delegate = self youtubeplayer.frame = frame youtubeplayer.loadWithVideoId(videoId, playerVars: playerVars as [NSObject : AnyObject]) _scrollview.addSubview(youtubeplayer) youtubeplayer.playVideo() youtubeplayer.playerState()

            continue
        }

        var _imageview = UIImageView()
        _imageview.frame = frame
        _imageview.contentMode = UIViewContentMode.ScaleAspectFill
        _imageview.image = UIImage(named: img_arr[p] )

        _scrollview.addSubview(_imageview)

    }

    frame.origin.x = _scrollview.frame.size.width * CGFloat(4)
    frame.size = _scrollview.frame.size

    _scrollview.contentSize = CGSizeMake(_scrollview.frame.size.width * CGFloat(4), _scrollview.frame.size.height)

    page_counts.numberOfPages = 4

    // Do any additional setup after loading the view, typically from a nib.
}
ghost commented 8 years ago

same problem here