Extracting data from an audio file - ios

We have a web services function to which we are uploading an audio file into to be saved in the server. The code that I am using to create a data buffer (to pass to the web services) of the contents of the audio file is :
NSURL *soundFileURL = [[NSURL alloc] initFileURLWithPath:fnWithSlash];
audioData = [NSData dataWithContentsOfURL:soundFileURL];
NSLog(#"audiodata%d",audioData.length);
Here fnWithSlash contains the full path of the file. However audioData.length is always 0.
What am i missing?
Thanks in advance for your response.

Try using NSData's dataWithContentsOfFile: instead..If the file is in your bundle it will look like the example below, otherwise construct the filePath of the sound file to your desired location..
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"yourSound" ofType:#"wav"];
audioData = [NSData dataWithContentsOfFile:filePath];
NSLog(#"audiodata%d",audioData.length);

Related

iOS Save file to non-private directory

Right now I am working on a project which uploads files from the system and I was wondering if it is possible to save NSData to a location that is not private. Basically when I save data right now the cooresponding url looks something like this:
file:///private/var/mobile/Containers/Data/Application/.../image.jpg
But when I get files back from the UIImagePicker the URL looks like this:
file:///var/mobile/Containers/Data/Application/.../image.jpg
How can I (if it is possible) save data to a directory not under private?
Just for reference this is the code I have to move files so far:
- (NSURL *)moveFileToTemporaryMemory:(NSURL *)url {
NSError *readingError, *writingError;
NSString *fileName = url.lastPathComponent;
NSURL *tempURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:fileName];
NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&readingError];
[data writeToURL:tempURL options:NSDataWritingAtomic error:&writingError];
return tempURL;
}
There's no difference -- /var is a symbolic link to /private/var, so both paths are equivalent.

+URLWithString: returns nil

Im using QLPreviewController to view word, excel,pdf etc. The problem is the file is in the server so I need to download it from a certain URL.
I'm using this code:
NSURL *dLurl = [NSURL URLWithString:[NSString stringWithFormat:#"%#",DLPathStr]];
NSData *data = [NSData dataWithContentsOfURL:dLurl];
for downloading files, It works for the PDF file but in the word and excel files it doesn't work I'm not able to download anything.
I fixed it by using this code it doesn't download anything because the URL is wrong because it contains spaces.
NSString *str = [DLPathStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *dLurl = [NSURL URLWithString:[NSString stringWithFormat:#"%#",str]];

NSData not accepting escaped / UTF8 encoded URL

I am trying to create an NSData object that contains the contents of a file in my main bundle.
NSString *chordPath = [[NSBundle mainBundle] pathForResource:chordName ofType:#"png"];
NSURL *chordURL = [NSURL URLWithString:[chordPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL];
UIImage *chordImage = [UIImage imageWithData:chordImageData];
Prior to using stringByAddingPercentEscapesUsingEncoding I was getting a nil URL, so I went ahead and fixed that by reencoding the string. Now I get a valid URL but the chordImageData object is nil for me. The file is definitely included in my main bundle (since I was able to get the URL to begin with) so I am wondering what's wrong.
EDIT:
Running this:
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL options:NSDataReadingMapped error:&dataCreationError];
Gets me this for the error:
po dataCreationError
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x7530a00
Looking around Google it seems that the URL is still not encoded correctly. Anyone know an additional step to make sure the encoding is valid?
Try using fileURLWithPath:, something like this:
NSString *chordPath = [[NSBundle mainBundle] pathForResource:chordName ofType:#"png"];
NSURL *chordURL = [NSURL fileURLWithPath: chordPath];
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL];
UIImage *chordImage = [UIImage imageWithData:chordImageData];
There should be no need to do percent escaping. The reason you're having problems is that you are using URLWithString: which is meant for "non-file" URLs.
You should use +fileURLWithPath: for file-based URLs:
NSString *chordPath = [[NSBundle mainBundle] pathForResource:chordName ofType:#"png"];
NSURL *chordURL = [NSURL fileURLWithPath:chordPath];
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL];
UIImage *chordImage = [UIImage imageWithData:chordImageData];

iOS dataWithContentsOfURL - what's happening behind the scenes

I've got some simple code (copy and pasted from SO) that loads a KLM (XML based) file into iOS's documents directory. I then display the loaded data on a map.
I realise that this is not a good way of downloading and saving the file - NSUrlConnection seems to be recommended so that the loading can be managed. But I'm new to all this and I'd like to understand what is happening in this case first.
Here's the code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.kml"];
// Download and write to file
NSURL *url = [NSURL URLWithString:#"http://www.domain.co.uk/kml-resource..."];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
NSURL *fileurl = [NSURL fileURLWithPath:filePath];
kmlParser = [[KMLParser alloc] initWithURL:fileurl];
.....
My questions are:
What happens while dataWithContentsOfURL is connecting/downloading - does the application just freeze and become unresponsive?
If I run my program in aeroplane mode the second time, it still seems to work. When does it decide it's OK to skip the download and writeToFile?
Does anyone know if it uses any caching between dataWithContentsOfURL and the server? ie. can I be sure that if I get a response, it is fresh data and hasn't just been sitting in safari/iOS's cache.
Many thanks
dataWithContentsOfURL is blocking method, so yeah, you shouldn't run that on main thread.
probably has internal timeout, but that is private... maybe 60 sec.
documentation doesn't say anything about caching, so I assume it doesn't cache at all.

Bundle subdirectory access

I'm try to implement ability to change interface in my program, something like themes. I'm decide to use bundles, so in my case they looks like this:
Theme1.bundle
Theme2.bundle
Every bundle have folders:
Theme1.bundle
graphic
pic1.png
pic2.png
sound
sound1.wav
sound2.wav
So I can get path to any required picture from bundle with this code:
path = [NSString stringWithFormat:#"%#.bundle/graphic/%#", selectedBundle, imageName];
When I get pictures from bundle there is no problem, path looks like this (I check it using breakpoints):
Theme1.bundle/graphic/pic1.png and I create UIImage
image = [UIImage imageNamed:path];
Pictures loaded without any problem.
But when I trying to play sound using AVAudioPlayer, I get path to sound by same way and path looks like this:
Theme1.bundle/sound/sound1.wav, but when I try to create NSData
sound = [NSData dataWithContentsOfFile:path];
it is not initialized.
When I try to keep sounds in main bundle and get it with
path = [[NSBundle mainBundle] pathForResource:#"sound1" ofType:#"wav"];
everything is OK, and path looks like this
/var/mobile/Applications/E8313E88-CE05-44B2-A80C-B05331D8596F/Myapp.app/sound1.wav
I cant understand why this works with pictures but not with sound?
Why I cant get sound from bundle?
Only one thing I can suggest - imageNamed: do some work to find right path to file and dataWithContentsOfFile: does not.
In your code instead of
sound = [NSData dataWithContentsOfFile:path];
Use
NSString * path = [NSString stringWithFormat:#"%#.bundle/sounds/%#", selectedBundle, soundName];
NSString *fullPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:path];
sound = [NSData dataWithContentsOfFile:path];
Explanation:
ImageNamed attaches the path of the application to the image name you specify

Resources