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
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];
NSString *path = [[NSBundle mainBundle] pathForResource:#"PTCL" ofType:#"mp4"];
NSURL *videoURL = [NSURL URLWithString:path];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayerController.view setFrame:self.view.frame];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.controlStyle=MPMediaTypeAnyVideo;
moviePlayerController.fullscreen = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
NSString *path = [[NSBundle mainBundle] pathForResource:#"PTCL" ofType:#"mp4"];
path=[NSString stringWithFormat:#"file:/%#",path];
NSURL *videoURL = [NSURL URLWithString:path];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayerController.view setFrame:self.view.frame];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.shouldAutoplay=YES;
moviePlayerController.controlStyle=MPMediaTypeAnyVideo;
moviePlayerController.fullscreen = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
file:///Users/utkal/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/F0B3AD63-7E46-4069-8845-8B0C05425CD2/CosMos.app/PTCL.mp4
I am getting this videURL path for first code
file:///Users/utkal/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/F0B3AD63-7E46-4069-8845-8B0C05425CD2/CosMos.app/PTCL.mp4
I am getting this videURL path for second code.
I have alos use
NSURL *videoURL = [[NSURL alloc]init];
videoURL = [[NSBundle mainBundle] URLForResource:#"PTCL" withExtension:#"mp4"];
But my video player always show that file is loading and nothing happen.
I know some where I am doing mistake,but hard luck of mine.
please correct my mistake or tell me if any another way to play local video file.Please.
maybe is too late but i would try something like:
MPMoviePlayerViewController *movieVC = [[MPMoviePlayerViewController alloc] initWithContentURL:file.location];
movieVC.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
movieVC.moviePlayer.fullscreen = YES;
movieVC.moviePlayer.allowsAirPlay = YES;
movieVC.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self presentMoviePlayerViewControllerAnimated:movieVC];
notice the "movieVC.moviePlayer.movieSourceType = MPMovieSourceTypeFile;"
I used this simple solution instead:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"video" ofType:#"mp4"]];
MPMoviePlayerViewController* viewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:viewController];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"CapturedMedia"];
NSString *filePath = [dataPath stringByAppendingPathComponent:#"/itemVideo.mp4"];
_moviePlayer =[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
[[_moviePlayer view] setFrame:[[self view] bounds]];
[[_moviePlayer moviePlayer] prepareToPlay];
[[_moviePlayer moviePlayer] setShouldAutoplay:YES];
[[_moviePlayer moviePlayer] setControlStyle:2];
[[_moviePlayer moviePlayer] setRepeatMode:MPMovieRepeatModeNone];
[self presentMoviePlayerViewControllerAnimated:_moviePlayer];
you set the property of the MPMoviePlayerViewController in .h file.
MPMoviePlayerController is Deprecated Now. So I have used AVPlayerViewController. and written the following code:
NSURL *videoURL = [NSURL fileURLWithPath:filePath];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];
Please do not forget to import following frameworks:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
Hope this Helps..
Your code for movie player is looking fine . There is something wrong with the path . Just try to go to the path explicitly . And check whether that file is present there or not. You can see following SO question for your reference :
How to play video using MPMoviePlayerController?
A little late in the game but here's my complete code
In *.h
#interface v1SupportTable : UITableViewController
{
MPMoviePlayerController *moviePlayer1;
}
In *.m
- (IBAction) playVideoBtn
{
NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"sample_movie" ofType:#"mp4"]];
NSLog(#"videoURL: %# ...", videoURL);
moviePlayer1 =[[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[moviePlayer1 view] setFrame: [self.view bounds]]; // frame must match parent view
[self.view addSubview: [moviePlayer1 view]];
[moviePlayer1 setFullscreen:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doneButtonClicked) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[moviePlayer1 play];
}
-(void)playMediaFinished:(NSNotification*)theNotification
{
moviePlayer1=[theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer1];
[moviePlayer1.view removeFromSuperview];
//when finished dimiss window
[moviePlayer1 stop];
[moviePlayer1.view removeFromSuperview];
}
-(void)doneButtonClicked
{
//[self.navigationController setNavigationBarHidden:NO animated:NO];
[moviePlayer1 stop];
[moviePlayer1.view removeFromSuperview];
}
in my app i want to play a video in an MPMoviePlayerController, but when i run the app it shows me only a white frame. here's the code:
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Problems" ofType:#"MP4"];
NSLog(#"path: %#", moviePath);
//NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
player.view.frame = CGRectMake(0, 0, 700, 700);
player.view.backgroundColor = [UIColor whiteColor];
player.controlStyle = MPMovieControlStyleDefault;
player.shouldAutoplay = YES;
[self.detailView addSubview:player.view];
[player play];
Edit: i tried this code instead and added an #property for the MPMoviePlayerController. i can see now the player but the app crashes and the breakpoint couldn't identify where. here's the code:
self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Problems" ofType:#"MP4"]]];
[self.player.view setFrame:CGRectMake(0, 0, 320, 320)];
[self.detailView addSubview:self.player.view];
[self.player play];
You should use the method 'presentMoviePlayerViewControllerAnimated:':
[self presentMoviePlayerViewControllerAnimated:self.player];