So I'm using ASIHTTPRequest to download a video file from my FTP. It downloads and I get this as the directory path to the file:
/Users/Matt_Sich/Library/Application Support/iPhone Simulator/5.0/Applications/B7366FF2-7592-4ED0-ABC2-46BA111D0FF4/Documents/INWD.mp4
Ok now I want to play that video.... I would image that doing this would be easy but for some reason I can't manage to get it to work. All I get is a black view and that's it.
//NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Park_iPod" ofType:#"mp4"]];
NSURL *url = [NSURL URLWithString:self.PathString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
(NOTE: self.PathString is the path to the downloaded file that I mentioned above)
It plays the video from the main bundle just fine so I'm sure that there's something wrong with the file path.
Once you've downloaded your file you need to use fileURLWithPath to access the file, not URLWithString
NSURL *url = [NSURL fileURLWithPath.PathString];
This would get you the documents directory...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:([NSURL fileURLWithPath : self.PathString])];
[moviePlayer setShouldAutoplay:NO];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(movieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: moviePlayer];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object: moviePlayer];
[self.view addSubview: moviePlayer.view];
[moviePlayer.view setFrame: self.view.bounds];
[moviePlayer prepareToPlay];
Related
When I use the MPMoviePlayerController in my app, it does not play videos and just shows a black rectangle.
my code is as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = nil;
//filePath =[NSString stringWithFormat:#"%#/%#.mp4", documentsDirectory, fileName];
filePath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.mp4",fileName]];
NSLog(#"%#",filePath);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (fileExists){
NSLog(#"exist!");
}else{
NSLog(#"no exist!");
}
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];
[moviePlayer.view setFrame:self.view.frame];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle=MPMovieControlStyleDefault;
moviePlayer.movieSourceType= MPMovieSourceTypeStreaming;
moviePlayer.fullscreen = YES;
[moviePlayer prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
-(void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
The output is:
iBeaconCollection[10320:60b] /var/mobile/Applications/107F6DE4-AE31-40E0-BAA0- 76A956B72116/Documents/101A ~ Hallway to 101-103.mp4
2014-06-11 14:13:27.302 iBeaconCollection[10320:60b] exist!
Hope someone could help me! Thanks.
Use MPMoviePlayerViewController, and then try and make sure that the file is in fact an MP4, as you are appending that extension.
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];
}
So I have a video being accessed by filepicker.io that is attempting to be played with an MPMoviePlayerController. So far I've tried everything from trying to load the video directly from the FilePicker URL to grabbing the NSData and loading it into the local filesystem. This is my current code, but all that happens is I get a blank screen and the [movieplayer load state] returns 0 (error code). I've verified that the file is there and that it is an MOV filetype.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
movieString = [documentsDirectory stringByAppendingPathComponent:#"vid.mov"];
[responseData writeToFile:movieString atomically:YES];
NSURL *url = [NSURL fileURLWithPath:movieString];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: url];
[moviePlayer prepareToPlay];
[moviePlayer.view setFrame: self.view.bounds];
[self.view addSubview: moviePlayer.view];
[moviePlayer play];
Declaring the movie player as a property fixed the problem. Apparently there's a bug associated with it.
I am trying this to load the video from the Project directory , can any one suggest , what exactly i am missing .
NSURL *myURL =[[NSBundle mainBundle] URLForResource:#"US_Very_High_Dive_Boudia_US_44_x264"
withExtension:#"mp4"];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player prepareToPlay];
[player shouldAutoplay];
[player allowsAirPlay];
[self.view addSubview:player.view];
[player setFullscreen:YES animated:YES];
player.controlStyle=MPMovieControlStyleEmbedded;
Try this code
In .h file add the following
#property (nonatomic, strong) MPMoviePlayerController *controller;
In .m file
-(IBAction)playMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"buyTutorial" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
[self setController:moviePlayerController];
}
Seems like You are not setting URL properly - try this:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"filename" ofType:#"mp4"]];
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];
// .....
}