AVAudioplayer not working after project rebuild - ios

I have an audio player that plays local files.
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]
It works fine. However if I click xcode run button to rebuild the app, the player is not working. It gives me Error
Error Domain=NSOSStatusErrorDomain Code=2003334207 "The operation couldn't be completed. (NSOSStatus error 2003334207.)"
The url is absolutely the same. I checked the file. It is where it says it is and it is not corrupt or anything.
Edit:
[[NSFileManager defaultManager] fileExistsAtPath:url.path]
returns false but if the file is there what this means is that I don't have access to it?

Likely, with iOS8, the path that you have saved won't be valid across launches.
The solution is to save the filename and not the complete path, and to recreate the URL or complete path, by getting the path to the Documents (or tmp) folder and appending the filename to it.

Related

ObjectiveC - Reading ePub File Gotten From Dropbox

So I'm pretty lost with this one and really new to epub files. I've done a bit of searching but can't seem to put everything together in my head.
My app uses DropBox's Chooser API to get a file from a user's DropBox folder. In this case, I want to open up a .epub file. So when the user chooses a file, the DropBox API gives me back an NSURL object to that file. For example:
https://dl.dropboxusercontent.com/1/view/e8bmxpkree6nc67/The%20Art%20of%20War.epub
And now, I've tried a couple different tools to try to read this file. Originally, I tried using KFEpubKit. But when I called:
epubURL; // The url from DropBox (shown above)
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
self.epubController = [[KFEpubController alloc] initWithEpubURL:epubURL andDestinationFolder:documentsURL];
self.epubController.delegate = self;
[self.epubController openAsynchronous:YES]
I would get back an error that the file couldn't be unzipped. The error reads as:
Epub Error: Error Domain=KFEpubKitErrorDomain Code=1 "Could not extract epub file." UserInfo=0x170275400 {NSLocalizedDescription=Could not extract epub file.}
I looked into the code and narrowed down the problem a little bit. The KFEpubKit uses the SSZipArchive utility to unzip files. And from this point on, I'm a bit stuck. The [SSZipArchive unzipFileAtPath: toDestination:] call seems to be failing when used with the epubURL.path. I'm not sure if this has something to do with the fact that my file is a .epub extension and not a .zip extension. Or maybe there's some stuff to do after getting the URL from DropBox and before giving it to the KFEpubKit tool?
In the end, I'm expecting to have to display the text of the book with a UIWebView. But I'm just not sure how to handle this .epub file. What should I do with the file from Dropbox? Any help is much appreciated.
A quick glance indicates that that SSZipArchive wants a local file URL, not a remote HTTP URL. Try downloading the file first (NSData with contents of URL, then write to some temp file) then create a file URL that points to the temp file, and send that into the KFEpubController:
// Download the file from dropbox
epubURL; // The url from DropBox (shown above)
NSData * epubData = [NSData dataWithContentsOfURL:epubURL];
NSString * tempPath = [NSTemporaryDirectory() stringByAddingPathComponent:#"temp.epub"];
[epubData writeToFile:tempPath atomically:YES];
NSURL *tempURL = [NSURL URLWithString:tempPath];
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
self.epubController = [[KFEpubController alloc] initWithEpubURL:tempURL andDestinationFolder:documentsURL];
// etc.
(Coding from memory.) All normal caveats apply here-- you'll want to do proper progress/error handling on the download, get rid of the temp file, etc, etc.

iPad saving audio save to local MusicDirectory

iOS 8.2
iPad a1474
inside app on IPAD, my code is
var dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
user records voice and it saves to
Optional(file:///var/mobile/Containers/Data/Application/2B2028C4-8E7D-4D7F-9FAF-EB843F506A5E/Documents/audio-2015-01-25-144545.m4a)
in Itunes/App,
have tried
MusicDirectory and SharedPublicDirectory
with either of
LocalDomainMask or DocumentDirectory
none work with error
The operation couldn’t be completed. (OSStatus error 2003334207.)
fatal error: unexpectedly found nil while unwrapping an Optional value
so what gives?
why can't app save to local MusicDirectory on the iPad??? there is nothing in the docs to indicate that this is protected area on the iPad.
no issues with iTunes or OSX, this is an iOS issue.
Under iOS8, the path that you have saved won't be the same across launches. The id you see "410AB24E-5FB0-4401-AC59-3C03D676E951" will change with each launch.
The workaround is to save the filename and not the complete path, and to recreate the URL or complete path, by getting the path to the Documents (or tmp) folder and appending the filename to it.

How to remove file from cache directory in iphone

I am working in a camera app where I can record video and save that in the directory like this:
file:///var/mobile/Applications/2683FFEC-63AB-4004-831B-DE38053CA472/Library/Caches/0C2E7E7C-5D78-4989-9C8E-259B3595BAC8-6498-0000042F24AB2D98.mov
when I go to the camera application I can see the video, but I want to delete this video when I quit the app, I did this so far
NSFileManager *fileManager = [[NSFileManager defaultManager];
[fileManager removeItemAtPath:THEABOVEURL error:&error];
but nothing happens and says cocoa error 4.The operation couldn’t be completed. No such file or directory
But I am sure that the file exists as I can play the video using the exact url mentioned above, what should I do now?
Cocoa Error 4 means
NSFileNoSuchFileError = 4, // Attempt to do a file system operation on a non-existent file
so it looks like that file just doesn't exist.
You may want to be sure that the file exists when you're going to delete it, maybe there is something wrong in the url that you are using?
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
it takes a string as first parameter, I see on your code
[vedioPaths objectAtIndex:THEABOVEURL]
it looks like THEABOVEURL should be an index, and vedioPaths contains all the NSString representing the path of the files, Add some NSLog to see if these variables contain the right information to delete the file.
Ok, I solved it my way, I was trying to delete an url where the
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
method wants NSUrl as parameter I converted this string as url and used this method
NSURL *urlfromString= [NSURL URLWithString:ABOVEURL];
[fileManager removeItemAtURL:urlfromString error:&error];
This solved my problem

Error while deleting the file

While deleting existing the file with this command:
[[NSFileManager defaultManager] removeItemAtPath:self.sourceFileName error:&error];
I got the following error
Error: ImageIO: CGImageReadCreateDataWithMappedFile 'open' failed '/Users/asdasd/Library/Application Support/iPhone Simulator/7.1/Applications/DD251D7D-F0AF-40E1-A033-F221623D589D/Library/ScanSession/Source/page3.jpeg
error = 2 (No such file or directory)'
This happens while I copied pic from album into app folder. The most interesting thing is that file exists, but not fully copied. Is there a way to check wether file is file operation completed?
check weather your file & Directory available
for (NSString *filename in files) {
NSString *path = [yourPath stringByAppendingPathComponent:yourFileName];
BOOL isDir;
if([[NSFileManager defaultManager] fileExistsAtPath:yourPath isDirectory:&isAvilDir] && isAvilDir){
NSLog(#"%# Check is a directory", your path file);
}
else {
NSLog (#"%# Check is a file",your path file);
}
}
I have a similar problem right now, and it seems as though when you delete a file that certain other methods you may have called immediately prior may actually not have completed yet. I'm considering delaying the actual deletion of files to allow for background processes to complete.
Solved it 2 yars ago. Forgot to post & close the question. It was caused by another thread, where the file was deleted first. I think its one of the standart mutlithreading issues while working with CoreData

fileExistsAtPath: returning NO for files that exist

At a point in my code fileExistsAtPath: is returning NO for files that I have confirmed exist. I've been scratching my head at this and can't figure out why its not working, so changed it to this code as this directory absolutely exists but if it doesn't gets created anyway.
NSError* err = nil;
NSURL *dir = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create: YES
error:&err];
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:[dir absoluteString]];
After running this code the application directory folder exists and err is 0, yet exists is NO.
How can this be?
TIA
You should use [dir path], not [dir absoluteString].
I was bashing my head against the wall for a few hours. Apparently on each and every run in xcode (on simulator) the app directory path was changing. The UUID part of it.
So instead of storing the fullpath I ended up persisting the path postfix
and prefixing that with whatever storage class is implied: temporary, cached
or documents :-[ You realize why you have to sometimes run on device
even if you don't explore the depths of Metal, GLES or multitouch? ;^)
There is a distinction between path and url. A path in unix representation is the location where your particular file or directory exists. Like "/Users/username/Desktop/myfile.txt"
Wheres a url not only contains the location but scheme as well, e.g: http:// or https:// and in our case a file url (file:///Users/username/Desktop/myfile.txt)
Methods like fileExistsAtPath or removeItemAtPath need path in the parameter rather than a url.
While methods like copyItemAtURL:toURL:error:(NSError * _Nullable *)error expects a url.
Use path attribute of NSURL object to retrieve the path. absoluteString gives you the path along with the scheme.

Resources