MPMoviePlayerController doesn't play from Documents folder - ios

Desperated.
Hello everybody!
I am having some issues with MPMoviePlayerController.
I've made it working with videos from NSBundle. But that's not what I need. I need to play it from Documents directory, because that is the place I store the recorded videos, wich URLs are stored in CoreData. but lets leave this aside, and simplify the code to its minimum needed. This code actually WORKS if using the contentURL, wich leads to NSBundle. After it, what I do to get to the docs place.
What I do:
NSURL *contentURL = [[NSBundle mainBundle] URLForResource:#"Oct_08_2012_10_00_51" withExtension:#"mp4"]; // this works
NSString* docPath = [NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * docaPathFull = [NSString stringWithFormat:#"%#%#", docPath, #"/Oct_08_2012_10_00_51.mp4"];
NSURL * docUrl= [NSURL URLWithString: docaPathFull];
BOOL ex = [[NSFileManager defaultManager] fileExistsAtPath:docaPathFull];
NSLog(#"file exists: %d, path using docPath: %#",ex, docaPathFull);
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:docUrl];
player.shouldAutoplay = YES;
player.controlStyle = MPMovieControlStyleEmbedded;
[player.view setFrame: self.thumbButton.bounds];
[player prepareToPlay];
[self.view addSubview: player.view];
[player play];
What we have:
2012-10-08 13:14:43.532 Voto[11968:907] file exists: 1, path using docPath: /var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Documents/Oct_08_2012_10_00_51.mp4
2012-10-08 13:14:43.907 Voto[11968:907] content URL: file://localhost/var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Voto.app/Oct_08_2012_10_00_51.mp4
2012-10-08 13:14:44.265 Voto[11968:907] doc URL: /var/mobile/Applications/07B8574A-A3BA-4B23-BB3F-995B33A01B95/Documents/Oct_08_2012_10_00_51.mp4
2012-10-08 13:14:45.343 Voto[11968:907] [MPAVController] Autoplay: Disabling autoplay for pause
2012-10-08 13:14:45.344 Voto[11968:907] [MPAVController] Autoplay: Disabling autoplay
2012-10-08 13:14:46.518 Voto[11968:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2012-10-08 13:14:46.540 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay
2012-10-08 13:14:46.554 Voto[11968:907] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2012-10-08 13:14:46.555 Voto[11968:907] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2012-10-08 13:14:46.557 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay
2012-10-08 13:14:46.567 Voto[11968:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2012-10-08 13:14:46.871 Voto[11968:907] [MPAVController] Autoplay: Enabling autoplay
So, the file exists...
Questions i've looked through:
MPMoviePlayer load and play movie saved in app documents
MPMoviePlayerController does not work with movie in documents folder
MPMoviePlayerViewController play movie from Documents directory - objective-c
I also checked ut with class reference, nothing specific about playing from Documents.
My projects settings:
using latest iOS 6,
Deployment target 5.0
Testing on both iOS6 iPhone simulator and an iPad with iOS 6.
If i forgot to add something, please remind me, I will do it immediately.
Please, help! :)

Well you'r not building the file URL the correct way, you should do it like this:
NSString *docPath = [NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *docaPathFull = [docPath stringByAppendingPathComponent:#"/Oct_08_2012_10_00_51.mp4"];
NSURL *docUrl= [NSURL fileURLWithPath:docaPathFull];
You should add directories and file to a path with the stringByAppendingPathComponent method of NSString;
Also when creating the file URL use fileURLWithPath: on NSURL, this willl create a correct NSURL for the giving path.

The most common mistake that everyone do is all use
NSURL *fileURL = [NSURL URLWithString:mVidPath];
^^^^^^^^^^^^^
instead of
NSURL *fileURL = [NSURL fileURLWithPath:mVidPath];
^^^^^^^^^^^^^^^

-(IBAction)playVideo
{
NSURL *vedioURL;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(#"files array %#", filePathsArray);
NSString *fullpath;
for ( NSString *apath in filePathsArray )
{
fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
vedioURL =[NSURL fileURLWithPath:fullpath];
}
NSLog(#"vurl %#",vedioURL);
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[player.view setFrame: self.view.bounds];
[player.moviePlayer prepareToPlay];
[self.view addSubview:player.view];
player.moviePlayer.controlStyle = MPMovieControlStyleDefault;
player.moviePlayer.shouldAutoplay = YES;
[player.moviePlayer setFullscreen:YES animated:YES];
[player.moviePlayer play];
[self presentMoviePlayerViewControllerAnimated: player];
}
Don't forget to add MediaPlayer.framework and
#import < MediaPlayer/MediaPlayer.h>
in your respective code.
Good Luck!!!

Related

don't show movie in document folder in xcode

I create one application that show file in document folder.
I have one file .mov format that I want show it from document folder but when run app and click play button don't show this movie and I see this image
:
this is my code : (please guide me and tell me my mistake)
- (IBAction)Play:(id)sender
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *file = [documentPath stringByAppendingPathComponent:#"xcode4-outlet.mov"];
NSURL *url = [NSURL fileURLWithPath:file];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[self.view addSubview:_moviePlayer.view];
_moviePlayer.fullscreen = YES;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.allowsAirPlay = YES;
[_moviePlayer play];
}
also compiler get me this massage :
movie player[9489:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
movie player[9489:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
movie player[9489:c07] [MPAVController] Autoplay: Enabling autoplay
movie player[9489:c07] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
movie player[9489:c07] [MPAVController] Autoplay: Enabling autoplay
You need to check whether a file is there or not before adding movie player also you can add default
-(IBAction)Play:(id)sender
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *file = [documentPath stringByAppendingPathComponent:#"xcode4-outlet.mov"];
NSURL *url = [NSURL fileURLWithPath:file];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:file];
if(fileExists)
{
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification
object:_moviePlayer];
_moviePlayer.shouldAutoplay=NO;
[_moviePlayer prepareToPlay];
}
}
Add your movie after movie is completely loaded
-(void)moviePreloadDidFinish:(NSNotification*)notification
{
_moviePlayer.controlStyle=MPMovieControlStyleDefault;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer play];
[_moviePlayer setFullscreen:YES animated:YES];
}
You may not be supplying a valid path to the URL. It looks like your trying to append your resource to the documents path, but that resource may not actually be there. You have to write the file to that path first. You should always double check your path with:
if ([[NSFileManager defaultManager] fileExistsAtPath:yourPath])
{
//path exists
}
Otherwise, just init the movie player with the straight up resource if you have it inside of your app.
Your code looks good. Check the file name is correct.. Also give a try like
-(void)playMovie
{
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:#"xcode4-outlet.mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
MPMoviePlayerController *moviePlayerController = [[[MPMoviePlayerController alloc] initWithContentURL:fileURL]retain];
[[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
{
// playback completed
}

Unable to play movie from document directory in iOS

I have this function to write NSData to document dictionary -
- (void)writeData
{
NSString *str = #"http://labs.widespace.com/resources/banner/ikea/verkligheten/video.m4v";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:str];
NSLog(#"%#",path);
NSLog(#"About to Write Data");
NSData* data0 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]];
[data0 writeToFile:path atomically:YES];
NSLog(#"Writting Data Completed");
}
Later i read this data and try to play the video using MPMoviePlayerController, but unfortunately this
does not play-
- (void)readData
{
NSString *str = #"http://labs.widespace.com/resources/banner/ikea/verkligheten/video.m4v";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:str];
NSURL* url = [[NSURL alloc] initFileURLWithPath:path];
NSLog(#"url:%#",url);
NSError *error = nil;
BOOL isReacahble = [url checkResourceIsReachableAndReturnError:&error];
NSLog(#"isReachable :%i,%#",isReacahble,error);
BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:path];
if(fileExist)
{
NSLog(#"File Exist in path");
}
else
{
NSLog(#"NO File Exist");
}
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer prepareToPlay];
NSLog(#"movie player:%#",moviePlayer);
[moviePlayer setControlStyle: MPMovieControlStyleFullscreen];
[moviePlayer.view setFrame:CGRectMake(0, 0,320, 480)];
[self.view addSubview:moviePlayer.view];
moviePlayer.shouldAutoplay = YES;
[moviePlayer play];
}
When i try to play this it shows following error-
2013-01-17 11:21:07.067 VideoCheck[338:907] [MPAVController] Autoplay: Disabling autoplay for pause
2013-01-17 11:21:07.068 VideoCheck[338:907] [MPAVController] Autoplay: Disabling autoplay
2013-01-17 11:21:07.090 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2013-01-17 11:21:07.091 VideoCheck[338:907] movie player:<MPMoviePlayerController: 0x21078250>
2013-01-17 11:21:07.093 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2013-01-17 11:21:07.102 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay
2013-01-17 11:21:07.113 VideoCheck[338:907] [MPAVController] Autoplay: Likely to keep up or full buffer: 0
2013-01-17 11:21:07.114 VideoCheck[338:907] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
2013-01-17 11:21:07.115 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay
2013-01-17 11:21:07.121 VideoCheck[338:907] [MPCloudAssetDownloadController] Prioritization requested for media item ID: 0
2013-01-17 11:21:07.149 VideoCheck[338:907] [MPAVController] Autoplay: Enabling autoplay
Can someone please run the code and provide some information about what's happening here??Please someone help me. I am using iOS 6.
In your wrieData method, instead of giving URL name as file, give only the name of the video. Like
NSString *path = [documentsDirectory stringByAppendingPathComponent:[str lastPathComponent]];
and do the same in readData method too.

MPMoviePlayerController video not playing

I used MPMoviePlayerController in my application.
I got the right URL but MPMoviePlayer does not play video it shows only black screen.
My code is as below:
[video setImage:[UIImage imageNamed:#"video_active.png"] forState:UIControlStateNormal];
NSString *filename=[NSString stringWithFormat:#"%#.mp4",[appdel.TempVideoUrl valueForKey:tag]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
NSURL *movieURL = [NSURL fileURLWithPath:filePath] ;
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.view.frame = CGRectMake(106, 18, 216, 235);
[uploadview addSubview:theMovie.view];
// Play the movie.
[theMovie pause];
I got the URL like this:
file://localhost/var/mobile/Applications/6E1D4CB7-A08D-438B-9FE7-E2FD9B7B5EEC/Documents/136_1355227629.mp4
Add [theMovie prepareToPlay]; before calling [theMovie play];.
prepareToPlay
Prepares a movie player for playback. (required)
- (void)prepareToPlay
Discussion
If a movie player is not already prepared to play when you call the
play method, that method automatically calls this method. However, to
minimize playback delay, call this method before you call play.
Calling this method may interrupt the movie player’s audio session.
For information on interruptions and how to resond to them, see Audio
Session Programming Guide. Availability
Available in iOS 3.2 and later.
Declared In MPMediaPlayback.h
Also you can use isPreparedToPlay for checking whether a movie player is ready to play.
Please refer this link for more.

MPMoviePlayerViewController hanging at loading screen

I have code that records the screen in my app, and then I want to be able to play it back. I have gotten so far as to get to the black loading screen with the rewind, play and fast forward buttons, but it hangs at loading without playing anything.
This code plays the clip:
-(IBAction)playMovie:(id)sender
{
MPMoviePlayerViewController *movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:locationOfVideoURL];
movieViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:movieViewController];
}
I believe the problem has something to do with the locationOfVideoURL variable. I am able to print it and it seems like it SHOULD work.
Here it is printed out:
file://localhost/Users/myusername/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/[left out, personal info]/Documents/Videoclip.mp4
Any ideas? Any help would be greatly appreciated! thank you
EDIT: Location of video clip:
NSString *outputPath = [[NSString alloc] initWithFormat:#"%#/%#", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0], #"Videoclip.mp4"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSLog(#"Completed recording, file is stored at: %#", outputURL);

MPMoviePlayerController does not work with movie in documents folder

I have a ViewController which plays a movie. If the movie was downloaded (with ASI-HTTP-Request) it takes the local version. Otherwise it streams it from the web. It's the exact same movie.
Streaming does work very well, but playing the local file does not work. Just a black screen and no controls. The file has been downloaded correctly I checked the md5 checksum.
It's a Apple QuickTime Video (.mov) at a size of 1024x5xx pixel.
player = [[MPMoviePlayerController alloc] init];
[player.view setFrame: self.view.bounds];
if (local) {
// DOES not work
// SHOULD be [NSURL fileURLWithString:...]
[player setContentURL: [NSURL URLWithString: #"/path/to/the/movie.mov"]];
} else {
// does work
[player setContentURL: [NSURL URLWithString: #"http://mydomain.tld/path/to/the/movie.mov"]];
}
[self.view addSubview: player.view];
[player play]
I tried around with the players MediaSourceType. But it didn't help.
Why does it not work?
I don't have any error messages because I dont receive one..., is there a way to get error messages from it?
Solution
I found my error.
I used [NSURL URLWithString: #"/the/path/..."] for the local path.
It works with [NSURL fileURLWithPath: #"/the/path/..."]
Check URL to video file. This line return path to Documents directory.
NSString* docPath = [NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
Your path is not related to Document directory
[player setContentURL: [NSURL URLWithString: #"/path/to/the/movie.mov"]];
Try this
[player setContentURL: [NSURL URLWithString: [NSString stringWithFormat:#"%#%#", docPath, #"/path/to/the/movie.mov"]]];

Resources