【问题标题】:using a webview to play videos from youtube in iphone app使用 webview 在 iphone 应用程序中播放来自 youtube 的视频
【发布时间】:2011-12-26 03:58:13
【问题描述】:

我正在尝试使用以下代码在我的 iphone 应用中观看视频:

-(void) viewDidLoad{
[super viewDidLoad];

UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame];

    NSString *youTubeVideoHTML = @"<html><head>\
    <body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";

    // Populate HTML with the URL and requested frame size
    NSString *html = [NSString stringWithFormat:youTubeVideoHTML,@"http://www.youtube.com/watch?v=ZiIcqZoQQwg" , 100, 100];

    // Load the html into the webview
    [webView loadHTMLString:html baseURL:nil];
    [self.view addSubview:webView];
}

谁能告诉我我做错了什么?我已经在几个例子中看到了这一点,但我仍然无法解决!

【问题讨论】:

    标签: iphone ios video uiwebview youtube


    【解决方案1】:

    您的 HTML 字符串格式似乎不正确。

    试试这个:

    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        float width = 200.0f;
        float height = 200.0f;
    
        NSString *youTubeURL = @"http://www.youtube.com/watch?v=ZiIcqZoQQwg";
    
        UIWebView *webView = [UIWebView new];
        webView.frame = CGRectMake(60, 60, width, height);
    
        NSMutableString *html = [NSMutableString string];
        [html appendString:@"<html><head>"];
        [html appendString:@"<style type=\"text/css\">"];
        [html appendString:@"body {"];
        [html appendString:@"background-color: transparent;"];
        [html appendString:@"color: white;"];
        [html appendString:@"}"];
        [html appendString:@"</style>"];
        [html appendString:@"</head><body style=\"margin:0\">"];
        [html appendFormat:@"<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\"", youTubeURL];
        [html appendFormat:@"width=\"%0.0f\" height=\"%0.0f\"></embed>", width, height];
        [html appendString:@"</body></html>"];
    
        [webView loadHTMLString:html baseURL:nil];
    
        [self.view addSubview:webView];
    
    }
    

    值得注意的是,这段代码只能在设备上运行,不能在模拟器中运行。

    更新...

    由于 YouTube 所做的更改,上述方法现在不正确。请改用下面的方法。

    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        float width = 200.0f;
        float height = 200.0f;
    
        NSString *youTubeToken = @"K95Q0VFyhA8";
    
        UIWebView *wv = [UIWebView new];
        webView.frame = CGRectMake(60, 60, width, height);
    
        NSMutableString *html = [NSMutableString string];
        [html appendString:@"<html>"];
        [html appendString:@"<head>"];
        [html appendString:@"<style type=\"text/css\">"];
        [html appendString:@"body {"];
        [html appendString:@"background-color: transparent;"];
        [html appendString:@"color: white;"];
        [html appendString:@"margin: 0;"];
        [html appendString:@"}"];
        [html appendString:@"</style>"];
        [html appendString:@"</head>"];
        [html appendString:@"<body>"];
        [html appendFormat:@"<iframe id=\"ytplayer\" type=\"text/html\" width=\"%0.0f\" height=\"%0.0f\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\"/>", width, height, videoToken];
        [html appendString:@"</body>"];
        [html appendString:@"</html>"];
    
        [wv loadHTMLString:html baseURL:nil];
    
        [self.view addSubview:wv];
    
    }
    

    【讨论】:

    • 在模拟器中不行吗?这是为什么?它不是在 webview 中嵌入播放器吗?
    • UIWebView 将被创建,但 YouTube 缩略图不会显示在其中,您将无法播放视频。这只发生在设备上。
    • 当我按下“完成”按钮时,它似乎从初始屏幕启动我的应用程序,而不是用视频关闭视图......你知道发生了什么吗?有没有办法改变按钮“完成”的行为?
    • 您的代码是否不适用于模拟器,或者我们无法在模拟器中播放 youtube 视频。我正在尝试在模拟器中玩游戏并在控制台上收到此错误。 "WebKit 丢弃异常 *** -[__NSCFString substringToIndex:]: Range or index out of bounds"
    【解决方案2】:

    替换网址。 使用

    http://www.youtube.com/v/XXXXXXX
    

    而不是

    http://www.youtube.com/watch?v=XXXXXXX
    

    【讨论】:

      【解决方案3】:

      试试这个简单的:

      //get the youtube video token from youtube url //https://www.youtube.com/watch?v=youTubeToken
      
       NSString *youTubeToken = @"K95Q0VFyhA8";
      
      //create webview..
          UIWebView *webView = [UIWebView new];
          webView.frame = CGRectMake(10, 10, 300, 300);
      
      //load webview with the load request..
          [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.youtube.com/embed/%@",youTubeToken]]]];
          [self.view addSubview:webView];
      

      【讨论】:

        猜你喜欢
        • 2011-11-16
        • 2012-04-05
        • 2011-04-25
        • 1970-01-01
        • 1970-01-01
        • 2011-06-28
        • 1970-01-01
        • 2012-02-22
        • 2011-08-27
        相关资源
        最近更新 更多