【发布时间】:2015-06-23 14:23:34
【问题描述】:
我尝试使用一个简单的 13 秒 mp4 视频作为登录屏幕的背景循环。
我希望视频自动播放和循环播放。它没有音频,我也不需要控件。
我需要它前面的按钮和其他对象。
我尝试使用 WebView 并将本教程中的 MP4 制作为 GIF 文件:https://medium.com/swift-programming/ios-make-an-awesome-video-background-view-objective-c-swift-318e1d71d0a2
但问题是我的 5 MB MP4(转换为 GIF)大小为 95 MB。
我不能用这个方法。
有没有“好用”的方法?
编辑:
好的,这就是我现在所做的。
我导入了 AVFoundation。
这是视图中的代码
- (void)viewDidLoad {
[super viewDidLoad];
NSURL* mURL = [[NSBundle mainBundle] URLForResource:@"App-BG-Loop" withExtension:@"mp4"];
AVPlayer* player = [AVPlayer playerWithURL:mURL];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[player currentItem]];
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = _videoView.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
playerLayer.needsDisplayOnBoundsChange = YES;
[_videoView.layer addSublayer:playerLayer];
_videoView.layer.needsDisplayOnBoundsChange = YES;
[player play];
// Do any additional setup after loading the view, typically from a nib.}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];}
这在 iOS 模拟器中效果很好,但是当我尝试在设备上启动它时,它不会启动或播放视频。视图只是保持白色。 No Error 什么都没有。
有什么想法吗?
编辑 2:
无法在设备上播放的问题是视频太大了。 Apple 仅在设备上支持 1080p。
【问题讨论】:
-
创建图像的 UIView 动画。这通常是最简单的方法
-
您可能想研究一种将 mp4 转换为 GIF 的更好方法,因为这比在后台处理循环视频要容易得多。或者正如@soulshined 提到的,创建一个带有图像的动画来制作你自己的 gif(各种)......stackoverflow.com/questions/6040528/…
-
感谢您的 cmets。我会尝试你的方法 tmrw 也许你可以检查我的编辑?
标签: ios objective-c video ios8 background