视频启动后,从UIWebView播放YouTube视频会崩溃

时间:2014-08-11 19:35:38

标签: ios objective-c video uiwebview youtube

播放视频2秒后,我的视频崩溃了。它给了我线程4 WebThread:EXC_BAD_ACCESS(代码= 2,地址0x20)

这就是我在做的事情..

UIWebView *videoView = [[UIWebView alloc] initWithFrame:CGRectMake(kElementX, kElementY, kElementWidth, 120)];
videoView.backgroundColor = [UIColor clearColor];
videoView.opaque = NO;
videoView.scalesPageToFit = YES;
videoView.delegate = self;

[cell.contentView addSubview:videoView];

NSString *videoHTML = [NSString stringWithFormat:@"\
                       <html>\
                       <head>\
                       <style type=\"text/css\">\
                       iframe {position:absolute; top:0%%; margin-top:0px;}\
                       body {background-color:#000; margin:0;}\
                       </style>\
                       </head>\
                       <body>\
                       <div id=\"player\">\
                       <iframe width=\"100%%\" height=\"420px\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>\
                       </div>\
                       <script>\
                       var tag = document.createElement('script');\
                       tag.src = \"https://www.youtube.com/iframe_api\";\
                       var firstScriptTag = document.getElementsByTagName('script')[0];\
                       firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\
                       var player;\
                       var myVideoId = document.getElementById('%@')\
                       function onYouTubeIframeAPIReady() {\
                       player = new YT.Player('player', {\
                       height: '100%%',\
                       width: '100%%',\
                       videoId: myVideoId,\
                       events: {\
                       'onReady': onPlayerReady,\
                       'onStateChange': onPlayerStateChange\
                       }\
                       });\
                       }\
                       function onPlayerReady(event) {\
                       event.target.playVideo();\
                       }\
                       function stopVideo() {\
                       player.stopVideo();\
                       }\
                       </script>\
                       </body>\
                       </html>", self.url, self.url];

[self.videoView loadHTMLString:videoHTML baseURL:nil];
[self.videoView setMediaPlaybackAllowsAirPlay:YES];

如果你们需要更多信息请告诉我..我尝试了不同的方法,但它一直在崩溃,我不知道为什么......

1 个答案:

答案 0 :(得分:0)

试试这个:

    NSString *exampleUrlString = @"https://www.youtube.com/embed/2bfP3TZHUzY?wmode=opaque&amp;rel=0&amp;autohide=1&amp;showinfo=0&amp;wmode=transparent";
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:exampleUrlString]]];
    [cell.contentView addSubview:webView];

<强>更新

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    NSString *exampleUrlString = @"https://www.youtube.com/embed/2bfP3TZHUzY?wmode=opaque&amp;rel=0&amp;autohide=1&amp;showinfo=0&amp;wmode=transparent";
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:exampleUrlString]]];
    [cell.contentView addSubview:webView];

    return cell;
}

不要忘记..

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //return the webView height
    return 150;
}

你现在看到Neymar吗?

enter image description here

enter image description here