【问题标题】:iOS 6 full screen local video using UIWebView and loadHTMLStringiOS 6 全屏本地视频使用 UIWebView 和 loadHTMLString
【发布时间】:2012-11-24 17:36:15
【问题描述】:

单击全屏按钮时,我在 UIWebView 中播放本地视频时遇到了一点问题。

这是一个 iPad 应用程序,当我转到包含视频的视图并单击播放按钮时,它会正常播放,我可以跳过和暂停/播放。但是当点击全屏时,它会将视频拉伸到全屏,播放一秒钟,全屏会消失,而不是在它的位置显示视频只是一个白框。我已经在运行 iOS 6 的 iPad 3rd Gen 和运行 iOS 6 的 iPad 模拟器上对此进行了测试,两者都做了同样的事情。但是,在运行 iOS 5 的第一代 iPad 和运行 iOS 5 的模拟器上,两者都可以全屏播放。

在 iOS 6 iPad/模拟器上,当我点击视频播放时,在调试区域中我看到以下内容:

2012-12-06 16:11:07.361 PICO[19131:11f03] [MPAVController] Autoplay: Enabling autoplay
2012-12-06 16:11:07.362 PICO[19131:11f03] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)
2012-12-06 16:11:07.362 PICO[19131:11f03] setting movie path: file:///Users/Nathan/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/DB4C3A91-F148-417B-8319-4690F89E1382/PICO.app/30.20.mp4
2012-12-06 16:11:07.362 PICO[19131:11f03] [MPAVController] Autoplay: Enabling autoplay
2012-12-06 16:11:07.368 PICO[19131:11f03] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2012-12-06 16:11:07.437 PICO[19131:11f03] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)
2012-12-06 16:11:07.447 PICO[19131:11f03] [MPAVController] Autoplay: Enabling autoplay
2012-12-06 16:11:07.448 PICO[19131:11f03] [MPAVController] Autoplay: _streamLikelyToKeepUp: 0 -> 1

然后当我点击全屏按钮时:

2012-12-06 16:12:39.918 PICO[19131:11f03] [MPAVController] Autoplay: Enabling autoplay
2012-12-06 16:12:40.168 PICO[19131:11f03] [MPAVController] Autoplay: Enabling autoplay
2012-12-06 16:12:40.178 PICO[19131:11f03] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 0, on player: 1)

这是我的代码:

.h

@property (strong, nonatomic) IBOutlet UIWebView *videoView;

.m

[videoView loadHTMLString:@"<body style=\"margin:0px;\"><video src=\"30.20.mp4\" controls width=\"640\" height=\"360\"></video></body>" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
videoView.scrollView.scrollEnabled = NO;

如您所见,我只是使用 loadHTMLString 来加载来自本地视频的视频标签。我在网上看得很清楚,但找不到任何关于此的内容。希望这只是一些简单的事情!

【问题讨论】:

  • 您只使用 UIWebView 来播放视频?为什么不是 MPMoviePlayerController 或 MPMoviePlayerViewController?
  • @lupatus 因为使用 UIWebView 我可以以我想要的大小定位我想要的位置,它会显示一个视图框架和一个播放按钮。单击该播放按钮时,它将在该 640x360 区域内播放,这是必要的,因为它旁边有所需的信息,并且它还将提供全屏选项。我可以用上面的两行代码来完成所有这些。它只是工作。我看了 MPMoviePlayer,但它似乎没有那么容易做任何事情,尽管证明我错了
  • 我要检查一下,我很久以前在玩视频播放器,但我认为它也像几行代码。
  • 有什么解决办法吗?我也遇到了同样的问题。
  • 嗨@NikitaP,如果您仍然卡住,请查看我的答案

标签: objective-c ios uiwebview ios6


【解决方案1】:

好的,我就是这样解决的。

添加 MediaPlayer 框架。 Target > Build Phases > Link Binary.. > + button

然后使用下面的代码。

YourClass.h
#import <MediaPlayer/MediaPlayer.h>
@interface YourClass : UIViewController
@property (strong, nonatomic) MPMoviePlayerController *moviePlayer; //creating the property is a must else it will show a black box instead of your video and will not play

YourClass.m
@synthesize moviePlayer; 
NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"myVideoFile" ofType:@"mp4"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
moviePlayer.view.frame = CGRectMake(73, 203, 640, 360); //position you want the player and it's size
[self.view addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer setShouldAutoplay:NO]; //autoplays by default

适用于 iOS 5.1 和 6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 2014-11-28
    • 2023-03-18
    相关资源
    最近更新 更多