Reputation: 1454
I do need to implement iOS application with video files played back in background.
E.g. on first viewController one video should be played back in background instead of some photo or color background. on second viewController another video should be played back in background instead of some photo or color background. and so on.
What is the best way to implement this? * is it better to import those video files in project? * or is it better to store them in some external place and playback via network?
From the AppStore approval point of view and from the Apple Guidlines point of view - is this case with video correct? Or it's better to avoid video usage in mobile applications?
Thank you in advance.
Upvotes: 0
Views: 45
Reputation: 1454
Found solution with local video files playback via native AVPlayer
from AVFoundation
1.Import AVFoundation:
#import <AVFoundation/AVFoundation.h>
2.Use property for player:
@property (nonatomic) AVPlayer *avPlayer;
3.Add video file into "Video" folder and added "Video" into project
4.Initialize the player
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"shutterstock_v885172.mp4" ofType:nil inDirectory:@"Video"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.view.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];
[self.avPlayer play];
5.Subscribe for event - video did play to the end
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]];
6.Resume video playback to the very start in related method
- (void)itemDidFinishPlaying:(NSNotification *)notification {
AVPlayerItem *player = [notification object];
[player seekToTime:kCMTimeZero];
}
Upvotes: 0