UIWebView播放本地和下载的视频

时间:2010-03-05 10:56:15

标签: iphone uiwebview nsbundle avcam

我想使用UIWebview播放下载的视频。我得到webkiterrordomain code = 204错误。 但如果我从资源文件夹播放视频,它运行完美。     //来自资源文件夹运行完美

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

谢谢。

4 个答案:

答案 0 :(得分:4)

解决方案就在这里,您可以在嵌入式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];

}

答案 1 :(得分:3)

WebkitErrorDomain 204只是意味着它会在MPInlinePlayerController上播放视频。所以你只需忽略错误就会播放视频。

答案 2 :(得分:0)

生成一个HTML。将视频嵌入其中。在项目资源中包含html和视频文件。然后从webview加载html。

答案 3 :(得分:-1)

要播放本地视频文件,您可以使用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];

}