【问题标题】:Issue while playing video in webview在 webview 中播放视频时出现问题
【发布时间】:2014-01-03 07:23:48
【问题描述】:

我在服务器上有视频,我正在使用 webview 播放视频,但是当我第一次点击完成时(在 webview 中)它会上升,而第二次它工作正常请建议我如何解决这个问题...

-(void)showPopViewWithUrl:(NSString*)url {
    self.navigationController.navigationBar.hidden = YES;
    float diff = [UIScreen mainScreen].bounds.size.height - self.view.frame.size.height;
    UIView* popView = [[UIView alloc] init];
    popView.frame = CGRectMake(0, -diff, self.view.frame.size.width, self.view.frame.size.height+diff);
    [popView setBackgroundColor:[UIColor yellowColor]];

    UIWebView* webview = [[UIWebView alloc] init];
    webview.backgroundColor = [UIColor clearColor];
    [webview setFrame:CGRectMake(0, 0, 300, 250)];

     UIButton* crossBtn = [UIButton buttonWithType:UIButtonTypeCustom];
     crossBtn.backgroundColor = [UIColor clearColor];
     [crossBtn addTarget:self action:@selector(crossBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
     [crossBtn setImage:[UIImage imageNamed:@"close_pop.png"] forState:UIControlStateNormal];
     [crossBtn setFrame:CGRectMake(popView.frame.size.width-30, popView.frame.size.height-40, 30, 30)];

    [popView addSubview:webview];
    [webview addSubview:crossBtn];

    [self.view addSubview:popView];
    [self playVideo:url webview:webview];
}

- (void)playVideo:(NSString *)urlString webview:(UIWebView*)videoView {
    NSString *embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    }\
    </style>\
    <script>\
    function load(){document.getElementById(\"yt\").play();}\
    </script>\
    </head><body onload=\"load()\"style=\"margin:0\">\
    <video id=\"yt\" src=\"%@\" \
    width=\"%0.0f\" height=\"%0.0f\" autoplay controls></video>\
    </body></html>";
    NSString *html = [NSString stringWithFormat:embedHTML, urlString,videoView.frame.size.width, videoView.frame.size.height];

    [videoView loadHTMLString:html baseURL:nil];
   // [self.view addSubview:videoView];

    NSLog(@"%@",html);
}

【问题讨论】:

    标签: ios objective-c cocoa webview


    【解决方案1】:

    -(void)showPopViewWithUrl:(NSString*)url { self.navigationController.navigationBar.hidden = YES; float diff = [UIScreen mainScreen].bounds.size.height - self.view.frame.size.height; UIView* popView = [[UIView alloc] init]; popView.frame = CGRectMake(0, -diff, self.view.frame.size.width, self.view.frame.size.height+diff); [popView setBackgroundColor:[UIColor yellowColor]];

    UIWebView* webview = [[UIWebView alloc] init];
    webview.backgroundColor = [UIColor clearColor];
    [webview setFrame:CGRectMake(0, 0, 300, 250)];
    
     UIButton* crossBtn = [UIButton buttonWithType:UIButtonTypeCustom];
     crossBtn.backgroundColor = [UIColor clearColor];
     [crossBtn addTarget:self action:@selector(crossBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
     [crossBtn setImage:[UIImage imageNamed:@"close_pop.png"] forState:UIControlStateNormal];
     [crossBtn setFrame:CGRectMake(popView.frame.size.width-30, popView.frame.size.height-40, 30, 30)];
    
    [popView addSubview:webview];
    [webview addSubview:crossBtn];
    
    [self.view addSubview:popView];
    NSString *html=[self playVideoFromUrl:url]
    [webview loadHTMLString:html baseURL:nil];
    

    }

    -(NSString *)playVideoFromUrl:(NSString *)strUrl{

    float width = 200.0f;
    float height = 200.0f;
    
    
    NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1];
    [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\"", strUrl];
    [html appendFormat:@"width=\"%0.0f\" height=\"%0.0f\"></embed>", width, height];
    [html appendString:@"</body></html>"];
    return html;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多