[AVPlayerViewController setPlayer:]: unrecognized selector sent to instance - ios

[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.

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];

Related

How to play all local videos consecutively

My app plays 5 short videos (stored in the app) by tapping 5 different buttons. I would like to have a "Play all"-button that plays all videos consecutively.
Here's my code for playing one video:
-(IBAction)playVideo1;
{
NSURL *videoURL = [[NSBundle mainBundle]URLForResource:#”video1” withExtension:#"mp4"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];
[self presentViewController:controller animated:YES completion:nil];
controller.view.frame = self.view.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:_currentItem];
}
I managed to find the answer myself thanks to the provided links and some googleing.
Here's how to play the videos in sequence using AVQueuePlayer:
- (IBAction)playAll:(id)sender {
NSString *firstVideoPath = [[NSBundle mainBundle] pathForResource:#”video1” ofType:#"m4v"];
NSString *secondVideoPath = [[NSBundle mainBundle] pathForResource:#"video2” ofType:#"m4v"];
NSString *thirdVideoPath = [[NSBundle mainBundle] pathForResource:#"video3” ofType:#"m4v"];
NSString *fourthVideoPath = [[NSBundle mainBundle] pathForResource:#"video4” ofType:#"m4v"];
NSString *fifthVideoPath = [[NSBundle mainBundle] pathForResource:#"video5” ofType:#"m4v"];
AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:firstVideoPath]];
AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:secondVideoPath]];
AVPlayerItem *thirdVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:thirdVideoPath]];
AVPlayerItem *fourthVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:fourthVideoPath]];
AVPlayerItem *fifthVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:fifthVideoPath]];
queuePlayer = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstVideoItem, secondVideoItem, thirdVideoItem, fourthVideoItem, fifthVideoItem, nil]];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = queuePlayer;
[self presentViewController:controller animated:YES completion:nil];
[queuePlayer play];

AVPlayer consuming a lot of CPU. Making app stuck randomly

I am developing a music player app and used AVPlayer for that, I have to play audio from document directory and also stream it directly. But my AVPlayer is consuming a lot of CPU making the app to stuck at random places.
App Flow
Initiated an AVPlayer
Checked whether the player is in memory or
not, if yes then replaceCurrentItemWithPlayerItem with the new audio
asset.
And for stream passed the URL for the asset
Any help I can get.
Any Please let me know what CPU consumption is normal for a music app.
Thanks
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentsDirectory = [pathArray objectAtIndex:0];
NSString* filePath = [documentsDirectory returnDocumentDirectoryPath:#"filename.mp3"];
NSURL *soundURL;
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
soundURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
}
if (_player != nil){
[_player pause];
[_player removeObserver:self forKeyPath:#"status"];
}
AVAsset *asset = [AVURLAsset URLAssetWithURL:soundURL options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
_player = [AVPlayer playerWithPlayerItem:anItem];
[_player addObserver:self forKeyPath:#"status" options:0 context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[_player currentItem]];
And while changing the track following code is used
[_player replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithAsset:[AVURLAsset URLAssetWithURL:soundURL options:nil]]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[_player currentItem]];
[_player play];
And while streaming online
if(!_player)
_player = [[AVPlayer alloc] init];
resourcePath=#"www.google.com/abc.mp3"//stream URL
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:resourcePath] options:nil];
NSArray *keys = [NSArray arrayWithObject:#"playable"];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^() {
if(!playerPlaying){
[_player replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithAsset:asset]];
}
}];

AVPlayerview comes from top left corner

I'm using AVPlayer to play videos. I have an issue on fullscreen mode. Video player comes from top left corner. It looks odd.
NSString *filePath = [self.video_Array objectAtIndex:index];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: nil];
url = [[NSURL alloc] initFileURLWithPath: soundFilePath];
currentItem = [AVPlayerItem playerItemWithURL:url];
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.videoGravity = AVLayerVideoGravityResize;
_playerViewController.view.frame = self.view.bounds;
_playerViewController.showsPlaybackControls = NO;
_video=[AVPlayer playerWithURL:url];
_video = [AVPlayer playerWithPlayerItem:currentItem];
_playerViewController.player = _video;
[_playerViewController.player play];
[self.view addSubview:_playerViewController.view];
Please refer my code which is working fine in my application,
-(void)videoPlayer:(NSString *)filePath{
playerViewController = [[AVPlayerViewController alloc] init];
self.viewPlayerContainer.frame = CGRectMake(0, 74, self.view.bounds.size.width, self.view.bounds.size.width);
NSURL *url = [NSURL fileURLWithPath:filePath];
AVURLAsset *asset = [AVURLAsset assetWithURL: url];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.player = player;
playerViewController.videoGravity = AVLayerVideoGravityResizeAspectFill;
[playerViewController.view setFrame:CGRectMake(0, 0, self.viewPlayerContainer.bounds.size.width, self.viewPlayerContainer.bounds.size.height)];
[playerViewController addObserver:self forKeyPath:#"videoBounds" options:NSKeyValueObservingOptionNew context:nil];
playerViewController.showsPlaybackControls = YES;
[self.viewPlayerContainer addSubview:playerViewController.view];
[player play];
}
You can use AVPlayerViewController to play fullscreen video
AVPlayerViewController+Fullscreen.h
#import <AVKit/AVKit.h>
#interface AVPlayerViewController (Fullscreen)
-(void)goFullscreen;
#end
AVPlayerViewController+Fullscreen.m
#import "AVPlayerViewController+Fullscreen.h"
#implementation AVPlayerViewController (Fullscreen)
-(void)goFullscreen {
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:self animated:YES completion:nil];
}
#end

Any one clarify regarding Avplayer playback controls

I created avplayer like this but controlls are not showing.can any one explain is there any playback controls are we need to create our custom playbacks.
NSString *path = [[NSBundle mainBundle] pathForResource:#"sample2" ofType:#"mp4"];
_videoURL = [NSURL fileURLWithPath: path];
_videoAsset = [AVURLAsset assetWithURL:_videoURL];
_videoDuration = (int)CMTimeGetSeconds(_videoAsset.duration);
_playerItem = [AVPlayerItem playerItemWithAsset:_videoAsset];
_player = [AVPlayer playerWithPlayerItem:_playerItem];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
_playerLayer.frame = CGRectMake(0, self.frame.size.height*0.2, self.frame.size.width, self.frame.size.height*0.3);
[self.layer addSublayer:_playerLayer];
[_player play];
_isPlaying = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:_playerItem];

Play a video from local itunes Library in a uiview frame

I have a MPMediaItem in which is a video of the local itunes library of the iPad.
how can I play in a UIView this now?
The following code does not work:
MPMediaItem *video = [allVideos objectAtIndex:indexPath.row];
NSURL *url = [video valueForProperty:MPMediaItemPropertyAssetURL];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player prepareToPlay];
[player.view setFrame: videoView.bounds];
[videoView addSubview: player.view];
[player play];
why does the following not work?
nothing happens...
MPMediaItem *song = [allVideos objectAtIndex:indexPath.row];
NSLog(#"%#",[song valueForProperty:MPMediaItemPropertyAssetURL]);
NSURL *url = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVPlayer *player = [[AVPlayer alloc] init];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:avAsset];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
player = [[AVPlayer alloc]initWithPlayerItem:playerItem];
//[player seekToTime:kCMTimeZero];
[player play];

Resources