Created
June 22, 2016 04:00
-
-
Save sbiermanlytle/14a6faab515f7691b810789086ae9e50 to your computer and use it in GitHub Desktop.
Disconnect AVPlayer with AVPlayerItem bug
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 "ViewController.h" | |
@import AVFoundation; | |
@interface ViewController () | |
@property (nonatomic) AVPlayerItem *sharedItem; | |
@property (nonatomic) AVPlayer *player; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
NSURL *videoURL; | |
videoURL = [NSURL URLWithString:@"https://s3.amazonaws.com/lookvideos.mp4/t/05093dabec6c9448f7058a4a08f998155b03cc41.mp4"]; | |
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil]; | |
self.sharedItem = [AVPlayerItem playerItemWithAsset:asset]; | |
self.player = [AVPlayer playerWithPlayerItem:self.sharedItem]; | |
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; | |
playerLayer.player.actionAtItemEnd = AVPlayerActionAtItemEndNone; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(videoDidPlayToEnd:) | |
name:AVPlayerItemDidPlayToEndTimeNotification | |
object:[playerLayer.player currentItem]]; | |
playerLayer.frame = self.view.bounds; | |
[self.view.layer addSublayer:playerLayer]; | |
[self.player play]; | |
} | |
- (void)videoDidPlayToEnd:(NSNotification*)note { | |
[self.player pause]; | |
[self.player replaceCurrentItemWithPlayerItem:nil]; | |
self.player = nil; | |
AVPlayer *player = [AVPlayer playerWithPlayerItem:self.sharedItem]; | |
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; | |
playerLayer.frame = self.view.bounds; | |
[self.view.layer addSublayer:playerLayer]; | |
[self.player play]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment