0

[AVPlayerViewController setPlayer:]: unrecognized selector sent to instance 0x101b91980 error.

I am trying to play a video using AVPlayerViewController.

@property (nonatomic) AVPlayer *avPlayer;
@property (nonatomic) AVPlayerViewController* avPlayerView;

NSString *path = [[NSBundle mainBundle] pathForResource:@"SampleVideo_1280x720_1mb" ofType:@"mp4"];

NSURL *url = [[NSURL alloc] initFileURLWithPath: path];

AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];

_avPlayer = [AVPlayer playerWithPlayerItem:anItem];
[AVPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
self.avPlayerView = [[AVPlayerViewController alloc] init];
self.avPlayerView.view.frame = self.view.bounds;
[self.avPlayerView setPlayer:_avPlayer];
[self.view addSubview:_avPlayerView.view];

but it is crashing. If i use same code in a new project, video play nicely.What is the problem?Please help.

MMR- Saad
  • 11
  • 7

1 Answers1

0

Make sure you import AVFoundation/AVFoundation.h and AVKit/AVKit.h in your controller and try to assign a player to playerController like this.

self.avPlayerView.player = self.avPlayer;

Instead of adding observer to AVPlayer Class add observer to Avplayer instance.

[avPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
Nauman Malik
  • 1,326
  • 9
  • 27
  • still crashing. – MMR- Saad Aug 21 '17 at 07:52
  • make sure file path is not empty and you may find help from this also. https://stackoverflow.com/questions/17807402/yet-another-unrecognized-selector-sent-to-instance-issue – Nauman Malik Aug 21 '17 at 08:10
  • No.... same crash. I think my problem is in frameworks which said by @CharlesSrstka . but i can't figure out. – MMR- Saad Aug 21 '17 at 08:38
  • I think it is this line: `[AVPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];` If you do this you need to implement `observeValueForKeyPath: ofObject: change: context:` in your `self` object. – Jonathan Oct 26 '17 at 22:13