Created
May 20, 2015 10:07
-
-
Save autresphere/0730d91d9fb4982123c6 to your computer and use it in GitHub Desktop.
Skeleton to create a looping video view.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "MyViewController.h" | |
#import <AVFoundation/AVFoundation.h> | |
@interface MyViewController () | |
@property (strong, nonatomic) IBOutlet UIView *movieView; | |
@property (strong, nonatomic) AVPlayer *player; | |
@end | |
@implementation MyViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view. | |
[self setupPlayer]; | |
} | |
- (void)setupPlayer | |
{ | |
AVPlayerLayer *playerLayer; | |
NSURL *url; | |
url = [[NSBundle mainBundle] URLForResource:@"video" withExtension:@"mp4"]; | |
playerLayer = [AVPlayerLayer layer]; | |
self.player = [AVPlayer playerWithURL:url]; | |
playerLayer.player = self.player; | |
playerLayer.frame = self.movieView.bounds; | |
[self.movieView.layer addSublayer:playerLayer]; | |
[self.player play]; | |
[self.player addObserver:self forKeyPath:@"rate" options:NSKeyValueObservingOptionNew context:nil]; | |
} | |
- (void)dealloc | |
{ | |
[self.player removeObserver:self forKeyPath:@"rate"]; | |
} | |
#pragma mark - KVO | |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context | |
{ | |
if((object == self.player) && (self.player.rate == 0)) | |
{ | |
[self.player seekToTime:kCMTimeZero completionHandler:^(BOOL finished) { | |
[self.player play]; | |
}]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment