Play a video from local itunes Library in a uiview frame - ios

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

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

How to get or streamed video file from cloudinary which are already uploaded ios

I have uploaded video on cloudinary Account succesfully, then cloudinary account provided url of uploaded video. I want to play and streamed that cloudinary url in Avplayer. So that I can watch full video.
uploaded video url provided by cloudinary as below-
"http://res.cloudinary.com/dep7kjzyg/video/upload/v1484562338/jol8unlgazw0awwhaidf.mov"
I have tried following code but cant play in Avplayer ios
NSString *urlString = #"http://res.cloudinary.com/dep7kjzyg/video/upload/v1484562338/jol8unlgazw0awwhaidf.mov";
NSURL *url = [NSURL URLWithString:urlString];
self.item = [AVPlayerItem playerItemWithURL:url];
self.player = [[AVPlayer alloc] initWithPlayerItem:self.item];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.item];
playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
playerLayer.backgroundColor = [UIColor greenColor].CGColor;
[self.view.layer addSublayer:playerLayer];
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.player play];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
NSURL *URL = [[NSURL alloc] initWithString:#"your string"]
if (URL)
{
AVPlayer *player = [AVPlayer playerWithURL:URL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[player play];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
}

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

AVPlayer play library video in view

i want to play a video in a frame of a view, but the following code does not work.
can you help me?
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];
You need to use AVPlayerLayer, add this
AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];
[self.view.layer addSublayer:playerLayer];
to your code.
Have you try to use MPMoviePlayerController as like below
MPMoviePlayerController * _player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
CGRect frame = self.frame;
frame.origin = CGPointZero;
_player.view.frame = frame;
[self.view addSubview:_player.view];
[_player prepareToPlay];
[_player play];
Try this one ..
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
player.view.frame = CGRectMake(0,45, 1024, 723); // take your require frame size of player
[player.view setBackgroundColor:[UIColor clearColor]];
player.repeatMode=MPMovieRepeatModeNone;
[self.view addSubview:player.view];
[player play];

Movie play error

I am trying play a video on device. Video is this
video
and my code:
NSString *urlStr = #"http://easyhtml5video.com/images/happyfit2.mp4";
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 320, 400);
[moviePlayer play];
When i start app an movie screen is seen but movie doesnt play. Can you help me where is the mistake ?
Try use
NSURL *url = [NSURL URLWithPath:urlStr];
instead of
NSURL *url = [NSURL fileURLWithPath:urlStr];
It is a web url not a file url.
Check this video for reference. This works for me -
- (IBAction)playMovie:(id)sender
{
NSString *filepath = #"http://easyhtml5video.com/images/happyfit2.mp4";
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}

Resources