Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbiermanlytle/14a6faab515f7691b810789086ae9e50 to your computer and use it in GitHub Desktop.
Save sbiermanlytle/14a6faab515f7691b810789086ae9e50 to your computer and use it in GitHub Desktop.
Disconnect AVPlayer with AVPlayerItem bug
#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