iOS 背景视频循环 GIF UIWebview 或 VideoPlayer
Posted
技术标签:
【中文标题】iOS 背景视频循环 GIF UIWebview 或 VideoPlayer【英文标题】:iOS Background Video Loop GIF UIWebview or VideoPlayer 【发布时间】:2016-09-17 10:31:25 【问题描述】:我需要编写一个应用程序,其中 View 的背景中有不同的短视频循环。这些循环通常应该有 10-30 秒的长度。我想知道在性能和内存经济性方面什么会更好(从应用商店下载时,循环应该与应用捆绑在一起)
a) 使用 UIWebview 作为背景,其中循环播放视频的动画 .gif b) 使用一些 VideoPlayerView (AVPlayer) 作为背景与类似 m4v 的文件 c) 一些我还没有考虑过的替代方案
也会有音频,但是音频没有链接到视频
【问题讨论】:
【参考方案1】:- (void)playIntroVideo
// NSBundle *bundle = [NSBundle mainBundle];
// // NSString *moviePath = iPadDevice ? [bundle pathForResource:@"IntroVideo" ofType:@"mp4"] : [bundle pathForResource:@"IntroVideo_iPhone" ofType:@"mp4"];
// NSString *moviePath = [bundle pathForResource:@"IntroVideo" ofType:@"mp4"];
// NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
CGRect frame = self.movieView.frame;
frame.origin.x=-200;
frame.size.width+=200;
AVAsset *avAsset = [AVAsset assetWithURL:movieURL];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:avAsset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *selectedImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
UIImageView *img = [[UIImageView alloc] initWithImage:selectedImage];
img.frame = frame;
[self.movieView addSubview:img];
AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
self.avplayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:self.avplayer];
[avPlayerLayer setVideoGravity:AVLayerVideoGravityResize];
if (iPhoneDevice)
[avPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[avPlayerLayer setFrame:frame];
[self.movieView.layer addSublayer:avPlayerLayer];
[self.avplayer seekToTime:kCMTimeZero];
// Not affecting background music playing
NSError *sessionError = nil;
[[AVAudiosession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
// [self.avplayer setVolume:0.0f];
[self.avplayer setActionAtItemEnd:AVPlayerActionAtItemEndNone];
- (void)viewDidLoad
[super viewDidLoad];
[self playIntroVideo];
- (void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
[self.avplayer play];
- (void)viewWillDisappear:(BOOL)animated
[super viewWillDisappear:animated];
[self.avplayer pause];
// self.avplayer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avplayer currentItem]];
- (void)playerItemDidReachEnd:(NSNotification *)notification
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
【讨论】:
以上是关于iOS 背景视频循环 GIF UIWebview 或 VideoPlayer的主要内容,如果未能解决你的问题,请参考以下文章