UIWebView 播放本地和下载的视频

发布于 2024-08-24 04:00:37 字数 428 浏览 16 评论 0原文

我想使用 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]];

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

方觉久 2024-08-31 04:00:37

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

}

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

}

神经大条 2024-08-31 04:00:37

WebkitErrorDomain 204 仅意味着它将在 MPInlinePlayerController 上播放视频。因此,您只需忽略该错误,视频就会播放。

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.

花开雨落又逢春i 2024-08-31 04:00:37

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

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.

独自唱情﹋歌 2024-08-31 04:00:37

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

}

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文