【问题标题】:Videos are not displaying in my YoutubePlayer视频未显示在我的 YoutubePlayer 中
【发布时间】:2015-01-09 04:43:27
【问题描述】:

我正在我的 WebView 上显示视频,视频 id 越来越完美,但视频没有显示在我的 WebView 上。这是我的代码

- (NSString *)htmlContent
 {

NSURL*link=[NSURL URLWithString:@"http://www.youtube.com/watch?v=LIuk1qu0RlU&feature=youtube_gdata_player"];
videoId = [[NSString alloc]init];
NSArray *queryComponents = [link.query componentsSeparatedByString:@"&"];
for (NSString* pair in queryComponents)
{
    NSArray* pairComponents = [pair componentsSeparatedByString:@"="];
    if ([pairComponents[0] isEqualToString:@"v"])
    {
        videoId = pairComponents[1];
        NSLog(@"Embed video id: %@", videoId);
        break;
    }
}

if (!videoId)
{
    [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Video ID not found in video URL" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil]show];

}

embedHTML = @"<!DOCTYPE html>\ <html>\ <head>\<style type='text/css' media='screen'>\body, p{\background-color: black;\margin: 0px;\}\#player {\width: 100%%;\}\</style>\</head>\<body><div id='player'></div>\<script type='text/javascript'>\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;\function onYouTubeIframeAPIReady() {\player = new YT.Player('player', {\height: '%f',\ width: '%f',\videoId: '%@',\events: {\'onReady': onPlayerReady,\}\});\}\\function onPlayerReady(event) {\event.target.playVideo();\}\\</script>\</body>\</html>";
CGSize playerSize = [self playerSize];
NSString *result = [NSString stringWithFormat:embedHTML,playerSize.width,playerSize.height,videoId];
return result;
 }


-(void)viewWillAppear:(BOOL)animated
{
       [self.playerwebview loadHTMLString:[self htmlContent] 
       baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];
}

谁能告诉我我在这里做错了什么。 谢谢

【问题讨论】:

    标签: ios uiwebview youtube-api nsurl


    【解决方案1】:
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    
    CGFloat screenWidth = screenRect.size.width;
    
    CGFloat screenHieght = screenRect.size.height;
    
    int width = (int) roundf(screenWidth);
    
    int height = (int) roundf(screenHieght);
    
    NSString *youTubeVideoHTML = @"<html><head><style>body{margin:0px 0px 0px 0px; }</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'%d', height:'%d', videoId:'%@', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
    
    NSString *html = [NSString stringWithFormat:youTubeVideoHTML,width,height,YouTubeId];
    
    [_webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
    

    用上面的代码块替换你的代码,我希望它对你有用

    【讨论】:

    • 不,它不适合我,视频不显示
    猜你喜欢
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2016-07-28
    相关资源
    最近更新 更多