UIWebView to Play Local and downloaded Video

99封情书 提交于 2019-11-27 06:25:58

问题


I want to play downloaded video using UIWebview. I get webkiterrordomain code=204 error. but if i play video from resources folder it run perfect. //from resources folder run perfect

NSString *tempurl = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"video.mp4"];
   //from downloaded file
 NSString *tempurl = downloaded path;
NSURL* urlLocation = [NSURL fileURLWithPath:tempurl];
[webView loadRequest:[NSURLRequest requestWithURL:urlLocation]];

Thank you.


回答1:


Solution is here, you can play video in Embedded UIWebView.

- (void)viewDidLoad {

[super viewDidLoad];

NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"http://www.businessfactors.de/bfcms/images/stories/videos/defaultscreenvideos.mp4\" type=\"application/x-shockwave-mp4\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";

webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 412.0)];

[webView setOpaque:NO];
NSString *html = [NSString stringWithFormat:embedHTML, webView.frame.size.width, webView.frame.size.height];
[webView loadHTMLString:html baseURL:nil];

[self.view addSubview:webView];

}




回答2:


WebkitErrorDomain 204 just means that it would play the video on an MPInlinePlayerController. So you just have to ignore the error and the video will play.




回答3:


Generate an html. Embed your video in it. Include both the html and the video file in the project resources. Then load html from webview.




回答4:


For playing local video file, you can use MPMoviePlayerController.

- (void) playVideo:(NSString *) videoName {
if ([self.fileManager fileExistsAtPath:videoName]) {
    [self stopVideo];
    self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:videoName]];
    [self.videoPlayer.view setFrame:CGRectMake(0, 0, 480, 360)];
    CGAffineTransform t1;
    t1 = CGAffineTransformMakeScale(-1.0,1.0);
    self.videoPlayer.view.layer.transform = CATransform3DMakeAffineTransform(t1);
    [self.view addSubview:self.videoPlayer.view];
    [self.videoPlayer setControlStyle:MPMovieControlStyleNone];
    [self.videoPlayer setScalingMode:MPMovieScalingModeAspectFit];
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(movieFinishedCallback:)
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:self.videoPlayer];
    [self.videoPlayer play];
}

}

- (void)stopVideo {
if (self.videoPlayer) {
    if ([self.videoPlayer playbackState] == MPMoviePlaybackStatePlaying) {
        [self.videoPlayer stop];
    }
    [self.videoPlayer.view removeFromSuperview];
    self.videoPlayer = nil;
}

}

- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
 removeObserver:self
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:player];
[self.videoPlayer.view removeFromSuperview];

}



来源:https://stackoverflow.com/questions/2386156/uiwebview-to-play-local-and-downloaded-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!