Downloading music files to the iphone - iphone

I am trying to develop an application which downloads online music data (music files) and makes them available when I am offline.
Can anyone help me? What is the best way to do that?

I like ASIHTTPRequest framework for downloading. You may also use standard NSURLRequest, but as for me it's not as easy-to-use as ASIHTTPRequest.
Update:
To save file, you may use Documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [paths objectAtIndex:0];
To save loaded data into a file, use
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
To load:
+ (id)dataWithContentsOfFile:(NSString *)path

Related

Why i can only save file or create folder at NSDocumentDirectory

I use ASI to download files.
But it only works if I set the path to NSDocumentDirectory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
I haven't changed any other code, just replaced NSDocumentDirectory to
NSCachesDirectory or NSDownloadsDirectory,and it does not work.
Download progress is 100% and file didn't save.
I don't know why.
This code:
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
works perfect in many projects of mine. I think the problem is in your saving code. Could you show it?
You can do it much easier and write to ANY location.
If you are using ASI, you can download DATA.(NSData)
So:
NSFileManager *fileMgr = [NSFileManager defaultManager];
[fileMgr createFileAtPath:#"whateverpathyouwant" contents:downloaddata attributes:nil];
This only works if you are jailbroken I guess.

How to save audio data recorded using AVAudioRecorder for further use?

Hi i am creating application which can record sound. I can have that recorded sound file and i can play it immediately. I do not know how can I save it on iPhone so that I can play it after reopening application.
Is there any way by which this file will be stored as application save data?
Thanks in advance.
Your can do it like this to record it into your documents directory :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"audioFile.ext"]; // Where ext is the audio format extension you have recorded your sound
[yourAudioPlayer.data writeToFile:filePath atomically:YES];

MPMoviePlayerViewController not playing a downloaded file

I've downloaded a file from a webserver, and saved it in my Documents Directory.
I try and pass the url of the file to MPMoviePlayerViewController using
initWithContentURL:url];
Where the url is created with
[NSURL URLWithString:#"/var/mobile/Applications/7A21A941-A54C-4116-857D-A34EDEE2F2E8/Documents/lesson.m4v"];
However whenever I try to play the video, the MoviePlayer appears for a second, with "loading" then dismisses itself.
Am I doing something wrong with the local file URL?
(The video plays fine when streamed from the webserver)
Thanks
I don't know why it is dismissing itself, but you actually are using web url instead of fileUrl. You need to initiate it like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"lesson.m4v"];
NSURL *url = [NSURL fileURLWithPath:path];
and pass this url to your MPMovieController
For local files must use:
[NSURL fileURLWithPath: somethingPath];
For server files can use:
[NSURL URLWithString: somethingPath];

How to add a ringtone from an application to ringtones of iphone?

I have created an application included a ringtone, but how can i add it to ringtones of iphone?
Use iTunes file sharing in your app and copy the ringtone file to the app's documents directory.
Set "Application supports iTunes file sharing" to YES in your info.plist
Wherever appropriate in your app copy out the file with the code below:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyRingtone" ofType:#"m4r"];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:filePath];
[[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:#"MyRingtone.m4r"]
contents:mainBundleFile
attributes:nil];
The user can now access the ringtone via itunes and add it to their device's ringtones.
You cannot. Apple doesn't release an API to export/write ringtones to the operating system.

how to place a file in application sand box in iphone

Is there any way of placing a file into application "sandbox" i.e into eg:
/var/mobile/Applications/30B51836-D2DD-43AA-BCB4-9D4DADFED6A2/Documents
of iphone?
My Application expects a file to be put into /Documents folder of application so that it can be uploaded to my local server.
This method should confirm to the rules of Apple so that it should not be rejected.
Why rejected? It is definitely normal to save a file to a Documents folder and Apple can not reject your application just because you save a file to the application Documents folder.
Use the following code to do so:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *pathToFile = [basePath stringByAppendingPathComponent:#"somefile.format"];
[someNSData writeToFile:pathToFile atomically: YES];