在webview中播放视频时出现问题

时间:2014-01-03 07:23:49

标签: ios objective-c cocoa 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);
}

1 个答案:

答案 0 :(得分:0)

- (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;

}