iOS play background video in a view - ios

I've added this code to the viewDidLoad method:
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"myvid" ofType:#"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.controlStyle = MPMovieControlStyleNone;
[self.view insertSubview: moviePlayer.view aboveSubview: self.view];
[moviePlayer play];
it doesn't work! how can I play a video in the background of a view in iOS? (using storyboards)

It looks like the instance of MPMoviePlayerController you create is wounded by ARC during the playback.
Try making the strong property of MPMoviePlayerController and assign the instance you create there before starting playback.
So your code will look like this
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"myvid" ofType:#"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
//set the frame of movie player
self.moviePlayer.view.frame = CGRectMake(0, 0, 200, 200);
[self.view addSubView:self.moviePlayer.view];
[self.moviePlayer play];

Related

How to play .mp4 video in MPMoviePlayer controller

Kindly tell me how to play a .mp4 format video play in MPMoviePlayerController
NSString *urlStr=[[NSBundle mainBundle]pathForResource:#"Tour.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0,154,720,428);
[moviePlayer play];
Add MediaPlayer Framework
import it to your file
#import <MediaPlayer/MediaPlayer.h>
Create an object of MPMoviePlayerController
MPMoviePlayerController * moviePlayer;
write this code where you wants to play video
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"test.mp4" ofType:nil];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayer.view];
moviePlayer.fullscreen = YES;
[moviePlayer play];
try above code...

How to play video using json web service data in iOS

I using the below code to play video file using web service,but it is not work.please correct the code to play video.
NSBundle *bundle = [NSBundle mainBundle];
NSString *url = [NSString stringWithFormat:#"http://tnhd.in/load/2014/Anjaan/Oru%20Kan%20Jaadai%20BRrip%20-%20Anjaan%20HD.mp4"];
NSString *moviePath = [bundle pathForResource:url ofType:#"mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayer.view.transform = CGAffineTransformConcat(moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[moviePlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:moviePlayer.view];
[moviePlayer play];
Just make the url like so:
NSString *path = #"http://tnhd.in/load/2014/Anjaan/Oru%20Kan%20Jaadai%20BRrip%20-%20Anjaan%20HD.mp4";
NSURL *url = [NSURL URLWithString:path];
Or better so:
NSURL *url = [NSURL URLWithString:#"http://tnhd.in/load/2014/Anjaan/Oru%20Kan%20Jaadai%20BRrip%20-%20Anjaan%20HD.mp4"];
and send it to your player

MPMoviePlayerController Not playing Local file

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

MPMoviePlayerController showing white background only

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

MPMoviePlayerController Doesn't Load Movie

I am using the following code:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"MovieName" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath isDirectory:NO];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
moviePlayerController.contentURL = fileURL;
moviePlayerController.fullscreen = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
However when the code is executed, the movie player appears, but the movie is never loaded.
For the sake of my own sanity, I added the following line:
NSData *thedata = [[NSData alloc]initWithContentsOfFile:filepath];
And I can verify the movie does exist, as it is loaded into the NSData object.
Where am I going working
TRY THIS :
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"MovieName" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath isDirectory:NO];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
moviePlayerController.view.frame = self.view.bounds;
moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
moviePlayerController.fullscreen = YES;
[self.view addSubview:moviePlayerController.view];
[moviePlayerController prepareToPlay];
[moviePlayerController play];

Resources