UIWebView to Play Local and downloaded Video
我想使用 UIWebview 播放下载的视频。我得到 webkiterrordomain code=204 错误。
但如果我从资源文件夹播放视频,它运行完美。
//从资源文件夹运行完美
1 2 3 4 5 | 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]]; |
谢谢。
解决方案来了,你可以在 Embedded UIWebView 中播放视频。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | - (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]; |
}
生成一个html。将您的视频嵌入其中。在项目资源中包含 html 和视频文件。然后从 webview 加载 html。
要播放本地视频文件,您可以使用 MPMoviePlayerController。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | - (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]; } |
}
1 2 3 4 5 6 7 8 | - (void)stopVideo { if (self.videoPlayer) { if ([self.videoPlayer playbackState] == MPMoviePlaybackStatePlaying) { [self.videoPlayer stop]; } [self.videoPlayer.view removeFromSuperview]; self.videoPlayer = nil; } |
}
1 2 3 4 5 6 7 | - (void) movieFinishedCallback:(NSNotification*) aNotification { MPMoviePlayerController *player = [aNotification object]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player]; [self.videoPlayer.view removeFromSuperview]; |
}