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];
Related
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];
I have a UIView which contains an AVQueuePlayer to show a videos. UIViewAnimationOptionTransitionCrossDissolve not working in this avqueue player
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url1 = [NSURL URLWithString:#"http:fff.mov"];
first = [AVPlayerItem playerItemWithURL: url1];
NSURL *url2 = [NSURL URLWithString:#"http:hgh.mov"];
second = [AVPlayerItem playerItemWithURL: url2];
NSURL *url3 = [NSURL URLWithString:#"http:jhk.mov"];
third = [AVPlayerItem playerItemWithURL: url3];
NSURL *url4 = [NSURL URLWithString:#"http:dsdfd.mov"];
fourth = [AVPlayerItem playerItemWithURL: url4];
player = [AVQueuePlayer queuePlayerWithItems:[NSArrayarrayWithObjects:first,second,third,fourth,nil]];
lists = [NSMutableArray arrayWithObjects:first,second,third,fourth, nil];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:playerLayer];
_displayview = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_displayview];
[player play];
for (int m;m<lists.count;m++)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:lists[m]];
}
}
-(void)itemDidFinishPlaying:(NSNotification *) notification
{
End=YES;
//
NSLog(#"view will Appear");
[UIView transitionWithView:_displayview
duration:3.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void)
{
NSLog(#"IT REACHED THE END");
}
completion:nil];
}
If anyone can help, I'd really appreciate any advice.
Thanks guys.
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];
would you please help me with this code, I'm trying to play video but I'm having error Exc_bad_Access code = 1 and some times code = 2:
-(IBAction)BtnPressed:(id)sender{
self.videoview.hidden = false;
NSString *btnTag = [NSString stringWithFormat:#"%d",[sender tag]];
NSString *videofilename = [NSString stringWithFormat:#"%#%#_%#", selectedGender, btnTag, selectedVowel];
//Playing video
NSString *filepath = [[NSBundle mainBundle] pathForResource:videofilename ofType:#"mp4"];
NSLog(#"file name is %#",filepath);
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
//NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:btnTag ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playercontroller];
//[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
//playercontroller.moviePlayer.scalingMode = MPMovieScalingModeNone;
playercontroller.moviePlayer.controlStyle = MPMovieControlStyleNone;
[playercontroller.view setFrame:CGRectMake(30, 50, 150, 200)];
[self.videoview addSubview:playercontroller.view];
[playercontroller.moviePlayer prepareToPlay];
[playercontroller.moviePlayer play];
//playercontroller = nil;
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
Waiting for your advice
It looks like your MPMoviePlayerViewController isn't retained, so it is probably getting deleted.
i want to play a video within a UIView using MPMoviePlayerController.
After pressing a UIButton, the video appears and starts playing.
Unfortunately after 3-4 seconds, the video gets black, the audio is still playing.
Any ideas?
Thanks for all your time.
(Using Xcode 4.2.1)
-(void) playMovieButtonPressed:(UIButton*) button{
NSString* video = #"testmovie.m4v";
NSString *filepath = [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController* player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
player.movieSourceType = MPMovieSourceTypeFile;
[player.view setFrame:CGRectMake(20, 400, 300, 200)];
[self.view addSubview:player.view];
[player prepareToPlay];
[player play];
}
- (void) moviePlaybackComplete:(NSNotification*) notification {
NSLog(#"videoviewcontroller complete: %#", notification);
MPMoviePlayerController *mymoviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:mymoviePlayerController];
}
I´ve talked to an Apple Engineer. The solution is that the player would have to be an instance variable or property of the view controller. So its still accessible when view was being torn down.
-(void) playMovieButtonPressed:(UIButton*) button{
NSString* video = #"testmovie.m4v";
NSString *filepath = [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
// .....
}