MPMoviePlayerViewController not working - objective-c

I have been roaming the internet for a solution I really don't know what am I doing wrong. I have been with this problem a few days now. I save the video at the following path that should be accessible to the application (Right?)
//NSDocumentDirectory doesn't work either.
NSArray *newPath = NSSearchPathForDirectoriesInDomains(NSMoviesDirectory,
NSUserDomainMask, YES);
NSString *moviesDirectory = [NSString stringWithFormat:#"%#/WebSurg",
[newPath objectAtIndex:0]];
// Check if the directory already exists
if (![[NSFileManager defaultManager] fileExistsAtPath:moviesDirectory]) {
// Directory does not exist so create it
[[NSFileManager defaultManager] createDirectoryAtPath:moviesDirectory
withIntermediateDirectories:YES attributes:nil error:nil];
}
I show the contents of this directory in a tableView in the application. When a row is tapped it should play the video. But it doesn't. It shows me the MPMoviePlayerViewController modal view and then hides it after probably what is 1 second. This is the code I use to play it:
I tried two ways of getting the path to no avail.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *moviesPath = NSSearchPathForDirectoriesInDomains(NSMoviesDirectory,
NSUserDomainMask, YES);
NSString *moviesDirectory = [NSString stringWithFormat:#"%#/WebSurg",
[moviesPath objectAtIndex:0]];
NSString *movie = [self.tableData objectAtIndex:indexPath.row];
NSString *moviePath = [NSString stringWithFormat:#"%#/%#",
moviesDirectory, movie];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
NSLog(#"MOVIEPATH: %#", moviePath);
NSString *alternatePath = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath], movie];
NSURL *alternateMoviePath = [NSURL fileURLWithPath:moviePath];
movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:alternateMoviePath];
movieViewController.moviePlayer.movieSourceType= MPMovieSourceTypeFile;
NSLog(#"Movie Load State: %d", [[movieViewController moviePlayer] loadState]);
NSLog(#"Alternate movie Path: %#", alternatePath);
[self presentMoviePlayerViewControllerAnimated:movieViewController];
[movieViewController.moviePlayer play];
[self checkAndPlay];
}
- (void) checkAndPlay {
if ([[self.movieViewController moviePlayer] loadState] == MPMovieLoadStateUnknown) {
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:
#selector(checkAndPlay) userInfo:nil repeats:NO];
} else {
[self.movieViewController setModalTransitionStyle:
UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:movieViewController animated:YES];
}
}
And these are the results of the console:
2012-10-08 10:14:52.392 WebsurgTemplates[3722:17903] MOVIEPATH: /Users/THISISME/Library/Application Support/iPhone Simulator/5.0/Applications/E075DBE3-BFEA-4F6A-9DFA-2CC912E14863/Movies/WebSurg/FirstVideo.mp4
2012-10-08 10:14:52.459 WebsurgTemplates[3722:17903] Movie Load State: 0
2012-10-08 10:14:52.460 WebsurgTemplates[3722:17903] Alternate movie Path: /Users/THISISME/Library/Application Support/iPhone Simulator/5.0/Applications/E075DBE3-BFEA-4F6A-9DFA-2CC912E14863/WebsurgTemplates.app/FirstVideo.mp4
I would greatly appreciate any suggestions and help!!
UPDATE
I made no progress so far. I managed to log some other data to the console, some info that may help more at solving this problem. I tried to make a blank project taking the direct download of the video as link to play the video but it didn't work. What happens is exactly the same thing. jaydee3 said that maybe it was due because I had probably no access to NSMoviesDirectory. So I changed to NSDocumentDirectory but that didn't solve the problem. I checked that the file exists and the format in which it is saved so it can be readable by the player. Still it doesn't work. I don't know what am I doing wrong. Thanks again for the suggestions/help.
Here the results of the debug. more complete:
if ([[NSFileManager defaultManager] fileExistsAtPath:moviePath]) {
NSLog(#"FILE EXISTS");
CFStringRef fileExtension = (__bridge CFStringRef) [moviePath pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
if (UTTypeConformsTo(fileUTI, kUTTypeImage)) NSLog(#"It's an image");
else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) NSLog(#"It's a movie");
else if (UTTypeConformsTo(fileUTI, kUTTypeText)) NSLog(#"It's text");
}
RESULTS
[6343:17903] MOVIEPATH: /Users/myname/Library/Application Support/iPhone Simulator/5.0/Applications/E075DBE3-BFEA-4F6A-9DFA-2CC912E14863/Documents/FirstVideo.mp4
[6343:17903] FILE EXISTS
[6343:17903] It's a movie

Okay so I managed to solve the problem and find the culprit.
In a brief note it was because the downloaded movie wasn't being saved properly (I am currently investigating the possible reasons why). And because of this the player was trying to play a file that existed, was in the correct format and the correct name but that was empty. I found this out by logging all the file sizes after download and transfer and play.
Now being more descriptive the issue was that I was downloading the movie to the NSCachesDirectory and then saving it to the NSDocumentDirectory. I found this because I started to wonder if it really found the file and if the file was "edible". It now plays the movie fine as I download it directly to the NSDocumentDirectory. Now I have to solve just in case the connection goes down. As saving in the NSCachesDirectory solved that automatically. I am open to suggestions on that. here is the code that didn't work to transfer the data from the NSCachesDirectory to NSDocumentDirectory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
// HERE I changed NSCachesDirectory to NSDocumentDirectory fixed it
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSString *downloadPath = [cachesDirectory stringByAppendingPathComponent:
#"DownloadedVideo.mp4"];
NSArray *newPath = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *moviesDirectory = [NSString stringWithFormat:#"%#",
[newPath objectAtIndex:0]];
self.downloadOperation = [ApplicationDelegate.mainDownloader downloadVideoFrom:
#"http://www.blablabla.web/iphone/download.php"
toFile:downloadPath];

Related

Downloading From Parse on Apple TV

The bounty expires in 3 days. Answers to this question are eligible for a +500 reputation bounty.
user717452 wants to draw more attention to this question.
I have a parse server set up, and as part of it, small PDFs (425KB) are stored on it. I need my Apple TV to be able to display these, but since they change often, it has to come from Parse server, and not just the main bundle where I update it with each update of the app. The issue I'm running into is the lack of an NSDocumentsDirectory on the Apple TV. How do y'all handle this? I've been using the Cache directory, but it seems to only work half the time with the code I am currently using. If I run it at launch from AppDelegate, by the time the PDF is needed, it may not be there, and if I have it set to run this code right when I need it, there is a delay, and sometimes, it simply doesn't show up. Would using NSTemporaryDirectory() be better? UPDATE, no, it doesn't. Works fine on simulator, on Apple TV, have to run the code two times to get it to both download, and draw the PDF
-(void)sermonTime {
//Check if PFFile exists, if so, display PDF, if not, blank time.
if ([self.entry[#"SermonPresIncluded"] isEqualToString:#"NO"]) {
[self blankTime];
}
else {
NSLog(#"SermonTime");
PFFileObject *thumbnail = self.entry[#"SermonPDF"];
[thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [[documentsDirectory stringByAppendingPathComponent:[self.entry valueForKey:#"DateOfService"]] stringByAppendingString:#".pdf"];
[imageData writeToFile:pdfPath atomically:YES];
NSURL *url = [NSURL fileURLWithPath:pdfPath];
self.view.backgroundColor = [UIColor blackColor];
self.arrayOfVerses = #[#"allverses"];
CGPDFDocumentRef pdfDocument = [self openPDF:url];
[self drawDocument:pdfDocument];
}];
}
}
-(void)sermonTime {
// Check if PFFile exists, if so, display PDF, if not, blank time.
if ([self.entry[#"SermonPresIncluded"] isEqualToString:#"NO"]) {
[self blankTime];
}
else {
NSLog(#"SermonTime");
PFFileObject *thumbnail = self.entry[#"SermonPDF"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [[documentsDirectory stringByAppendingPathComponent:[self.entry valueForKey:#"DateOfService"]] stringByAppendingString:#".pdf"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:pdfPath]) {
// Use cached copy of PDF
NSURL *url = [NSURL fileURLWithPath:pdfPath];
self.view.backgroundColor = [UIColor blackColor];
self.arrayOfVerses = #[#"allverses"];
CGPDFDocumentRef pdfDocument = [self openPDF:url];
[self drawDocument:pdfDocument];
} else {
// Download and save the PDF
[thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (error) {
// Handle the error
NSLog(#"Error downloading PDF: %#", error);
[self blankTime];
} else {
[imageData writeToFile:pdfPath atomically:YES];
// Use completion block to signal that the PDF is ready to display
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [NSURL fileURLWithPath:pdfPath];
self.view.backgroundColor = [UIColor blackColor];
self.arrayOfVerses = #[#"allverses"];
CGPDFDocumentRef pdfDocument = [self openPDF:url];
[self drawDocument:pdfDocument];
});
}
}];
}
}
}
Made some changes to the code.
It will first check if the PDF exists cache, it will use the PDF if it exists in cache and will only proceed download if it does not exists. Then, to make sure that PDF is downloaded and saved successfully you can use a completion block. With completion block, it will only proceed to draw it when the block is called to avoid the PDF does't show up.

Objective c - picker to get video path

In my app, I allow the user to record videos and I save them in this path:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths[0] stringByAppendingPathComponent:DestFileName];
I want to implement a video picker in my app, but the objective is to get the path of the video. I started with the basic method like this:
UIImagePickerController *videoPicker = [[UIImagePickerController alloc] init];
videoPicker.delegate = self;
videoPicker.modalPresentationStyle = UIModalPresentationCurrentContext;
videoPicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];‌​
videoPicker.mediaTypes = #[(NSString*)kUTTypeMovie, (NSString*)kUTTypeAVIMovie, (NSString*)kUTTypeVideo, (NSString*)kUTTypeMPEG4];
videoPicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
[self presentViewController:videoPicker animated:YES completion:nil];
The first problem is that I can not access to my recorded videos, why?
Then, this picker allows the user to play video, and when I choose one video I think that a compressed video is created and sent to our delegate method (the URL goes to a tmp repository).
I don't want to allow the user to play video and I just want the video path, is it possible with this method?
I can also get the list of files in
NSSearchPathForDirectoriesInDomains
with this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *rootPath = path[0];
[[NSFileManager defaultManager] createDirectoryAtPath:rooPath withIntermediateDirectories:YES attributes:nil error:nil];
NSArray* mPaths = [[NSFileManager defaultManager] contentOfDirectoryAtPath:rootPath error:nil];
With this method I can found my recorded videos, but then I have to implement my own custom picker, but it can be difficult and long to implement (need to get the thumbnail for example, possible?).
To summarize:
With the UIImagePickerController, is it possible to prevent the play, to just get the path and to show our recorded videos?
With the NSFileManager, is it possible to easily create a custom video picker with thumbnail, duration etc.)
Thanks for your help
UIImagePickerController is an instance of the iOS Photos App by which you can access all of the Photos/Videos in the Device Gallery and not exclusively those belonging to your App. There is no way to have the UIImagePickerController show only the videos recorded by your App. It is also not possible to have the UIImagePickerController skip the Play/Choose step while picking a File.
Like you mentioned you can definitely write the Video file into the Apps documents directory by means of NSFileManager and hold a reference to the path which you can use for later use. I think you would probably end up creating a Model Class say "Video" with the following attributes;
a) savedPath
b) duration
c) thumbnail etc
You can then use something like a NSKeyedArchiver to encode these objects and store into a File so that you can retrieve this information later.
I finally found the solution using a custom picker, :
To get the list of local videos:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* rootPath = paths[0];
[[NSFileManager defaultManager] createDirectoryAtPath:rootPath withIntermediateDirectories:YES attributes:nil error:nil];
NSArray* mPaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:rootPath error:nil];
To get the duration and the thumbnail:
NSURL *videoURl = [NSURL fileURLWithPath:mPath];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURl options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generate.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
UIImage *img = [[UIImage alloc] initWithCGImage:imgRef];
[m_videoFrames addObject:img];
CMTime duration = [asset duration];
int seconds = ceil(duration.value/duration.timescale);
Thanks!

Unable to import pages and numbers documents with UIDocumentPicker

I'm developing an app that provides the ability to store cloud documents.
This app will have the option to import data from other apps using the new UIDocumentPickerViewController.
Everything works fine and I'm able to show the picker view controller.
This is the code that I'm using to import the file:
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
if (controller.documentPickerMode == UIDocumentPickerModeImport) {
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString* path=[NSString stringWithFormat:#"%#/MyHandyTap/%#/%#",documentsDirectory,self.cartella,[url lastPathComponent]];
BOOL startAccessingWorked = [url startAccessingSecurityScopedResource];
NSURL *ubiquityURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
__block NSData *data;
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
NSError *error;
[fileCoordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
data = [NSData dataWithContentsOfURL:newURL];
[data writeToFile:path atomically:YES];
}];
[url stopAccessingSecurityScopedResource];
}
}
With this code I'm able to import a lot of different file formats (.pdf, .txt, .rtf, .doc etc) however if I try to import files .pages or .numbers files from iCloud the call to [NSData dataWithContentsOfURL:newURL] returns null.
Is it possibile to let users import these kinds of files?
I've red the documentation listed here https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/FileProvider.html#//apple_ref/doc/uid/TP40014214-CH18-SW2
and I've downloaded the example listed here : https://developer.apple.com/devcenter/download.action?path=/wwdc_2014/wwdc_2014_sample_code/newboxanintroductiontoiclouddocumentenhancementsinios8.0.zip
however I'm not able to figure out how to solve this issue.
Thank you in advance for your help
Andrea
Even I did not test it, but likely you get the problems, because documents of type .pages or .numbers are no single files, but bundles. So you cannot read them with -dataWithContentsOfURL:.
Did you check copying with -copyItemAtURL:toURL:error: (NSFileManager)?

can't save plist. path is not writable

I'm saving a lot of informations in a plist. This one is by standart in my mainBundle.
this is my method to load the path and the data from the plist. if the file in the "application support" folder doesn't exist, i'm copying it from the mainBundle to there.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
self.plistPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",plistName]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: self.plistPath])
{
NSString *pathInBundle = [[NSBundle mainBundle] pathForResource:plistName ofType:#"plist"];
self.plist = [NSMutableDictionary dictionaryWithContentsOfFile:pathInBundle];
NSLog(#"plist doesnt exist");
}
else {
self.plist = [NSMutableDictionary dictionaryWithContentsOfFile:self.plistPath];
NSLog(#"plist exist");
}
NSLog(#"plist path: %#",self.plistPath);
if i add the following lines at the end, there's only NO the answer:
if([fileManager isWritableFileAtPath:self.plistPath]) NSLog(#"YES");
else NSLog(#"NO");
after all, i tried to save with [self.plist writeToFile:self.plistPath atomically:YES];, which is also not working.
sorry for answering so late - i had a lot of other stuff to do. back to my problem: i only get the error, when i try to add a new entry to my dictionary (plist). editing is no problem. i think the problem is, how i try to add the entry. my code looks like:
NSMutableDictionary *updateDict = [[self.plist objectForKey:#"comments"]mutableCopy];
NSMutableDictionary *tmpDict = [[[NSMutableDictionary alloc]init]autorelease];
[tmpDict setObject:comment forKey:#"comment"];
[tmpDict setObject:author forKey:#"author"];
[tmpDict setObject:car forKey:#"car"];
[tmpDict setObject:part forKey:#"part"];
[tmpDict setObject:date forKey:#"date"];
[updateDict setObject:tmpDict forKey:[NSNumber numberWithInt:[updateDict count]+1]];
[self.plist setObject:updateDict forKey:#"comments"];
if([self.plist writeToFile:self.plistPath atomically:YES]) {
return YES;
}
else {
return NO;
}
self.plist is my local copy of the file at plistPath. the structure of my plist looks like: https://img.skitch.com/20111026-tcjxp9ha4up8ggtfjy7ucgqcqe.png
hope this helps
Ok, so that's not the Documents directory and iOS doesn't have an Application Support directory created in the sandbox by default, which is why you can't write.
You can either change your method call to look-up the real documents directory:
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
Or, after you get the path to the Application Support directory, you must check to see if it exists already and if not, create it.
please go through the previous post which shows the different way to copy the plist from mainBundle. Use [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; method instead.
Did you find answer? if not, you need to change this line:
[updateDict setObject:tmpDict forKey:[NSNumber numberWithInt:[updateDict count]+1]];
to
[updateDict setObject:tmpDict forKey:[NSString stringWithFormat:#"%d",[updateDict count]+1]];
Key name is string, not object.

NSFileManager FileSize Problem - Cocoa OSX

I have a function that checks the size of several plist files in the /User/Library/Preferences/ directory. For testing purposes, I'm using iTunes, which on my machine has a preference file of ~500kb.
EDIT: I have corrected my code as per the answer - as posted, this code works correctly.
NSString *obj = #"iTunes";
NSString *filePath = [NSString stringWithFormat:#"/Applications/%#.app",obj];
NSString *bundle = [[NSBundle bundleWithPath:filePath] bundleIdentifier];
NSString *PropertyList=[NSString stringWithFormat:#"/Preferences/%#.plist",bundle];
NSString* fileLibraryPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:PropertyList];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fileLibraryPath];
if (fileExists) {
NSError *err = nil;
NSDictionary *fattrib = [[NSFileManager defaultManager] attributesOfItemAtPath:fileLibraryPath error:&err];
if (fattrib != nil){
//Here I perform my comparisons
NSLog(#"%i %#", [fattrib fileSize],obj);
}
}
However, no matter what I do, the size is returned as 102. Not 102kb, just 102. I have used objectForKey:NSFileSize, I have used stringValue, all 102.
As stated in the selected answer below lesson learned is to always check the path you're submitting to NSFileManager.
Thanks!
The filePath that you are using in
NSDictionary *fattrib = [ ... attributesOfItemAtPath:filePath error:&err];
appears to be
/Applications/iTunes.app
which on my system is a directory of size 102 bytes, same for /Applications/Mail.app - 102 bytes. Is it just that the path is not what you intend?