How to download an audio file programmatically?

I want to download the audio file from the following link

link to audio file

I tried the other answers that are posted on this site. But I do not want to use ASIHTTPRequest, so someone can tell me please how to upload the contents of this link to my document directory.

+8
objective-c iphone xcode
source share
2 answers

Ok, you could write something like this:

//Download data NSData *data = [NSData dataWithContentsOfURL:<YOUR URL>]; //Find a cache directory. You could consider using documenets dir instead (depends on the data you are fetching) NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; //Save the data NSString *dataPath = [path stringByAppendingPathComponent:@"filename"]; dataPath = [dataPath stringByStandardizingPath]; BOOL success = [data writeToFile:dataPath atomically:YES]; 
+5
source share

You can use NSURLConnection to load a file into memory and then write it to a file.

 [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:yourURL] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){ [data writeToFile:yourPath atomically:YES]; // Audio file is ready to be played. }]; 

You can also create your own instance of NSOperationQueue to record in the background.

If the file is large and you do not want to store it entirely in memory, you should use AFNetworking , otherwise you could write your own implementation using NSOutputStream to write directly to disk.

0
source share

All Articles