19

I am developing an iPhone app for music. I want to give some options to the user so they can listen the song/music by streaming or they can download the music in the app. I know how to stream the audio files in the app programmatically. But, i don't know how to download the audio files in the app and play the audio after download. And also user can pause the download while it is in download. Is it possible to do ? Can anyone please guide me and please share any sample code or tutorial for me? I have one more doubt: Can you please help me to create a folder inside of the app? Please help me. Thanks in advance.

Fran Sevillano
  • 8,103
  • 4
  • 31
  • 45
Gopinath
  • 5,392
  • 21
  • 64
  • 97

2 Answers2

52

Creating a Folder

For every app you have a Documents Folder. Any files you use in your app are stored here. If you want to create more directories here, then you'd do something like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyNewFolder"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder if it doesn't already exist

Downloading

If you want to download audio files from the internet, then you want to look at an asynchronous method of doing it. This means that the application can carry on doing things while your download happens in the background.

For this, I recommend you use ASIHTTPREQUEST. It's great for carrying out any download requests, providing you with many options such as pausing a download (as you requested).

The website provides a great deal of documentation on downloading and storing a file. Take a look at the Downloading a File section on this page.

You'd just do something like:

NSURL *url = [NSURL URLWithString:@"http://www.fileurl.com/example.mp3"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:[NSString stringWithFormat:@"%@",dataPath]]; //use the path from earlier
[request setDelegate:self];
[request startAsynchronous];

Then just handle the progress and completion through the delegate methods that ASIHTTPRequest offers.

Playing

Playing the file is very simple. Once it's on the device and you know where it is, just load it into the audio player.

There's a great tutorial here on using AVAudioPlayer to play sounds. A very simple example of using it in your case would be:

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3",dataPath];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

if (audioPlayer == nil)
    NSLog([error description]);
else
    [audioPlayer play];
Liam George Betsworth
  • 18,373
  • 5
  • 39
  • 42
  • 1
    Great comment, well laid out explanation! +1 – onetwopunch Oct 29 '12 at 23:13
  • 1
    +1 for such a great answer :) ,this should be accepted, wondering why people ask question and disappear :( – swiftBoy Jan 29 '13 at 08:12
  • 4
    I recommend using AFNetworking rather than ASIHTTPRequest, nevertheless what an excellent answer @LiamGeorgeBetsworth – GangstaGraham Jul 19 '13 at 21:46
  • I see lot of issues with ASHttpRequest,wonder why there is no simple way to download the images from server to documents directory. I have implemented downloading files from server,however trying to save to documents directory is not smooth,the files are getting saved in corrupt fashion. Please review my code [here](http://pastebin.com/EbR4kMdr) and kindly help me,thanks :) – Eshwar Chaitanya Feb 24 '14 at 13:39
  • 1
    And yes i agree with @GangstaGraham that he should use AFNetworking, but please look at the date. AFNetworking classes was not available back then. – try catch finally Jun 20 '14 at 14:07
  • 1
    @EshwarChaitanya Its better you use AFNetworking, because ASIHTTPRequest classes has a lot of bug which breaks the app very often. Almost everybody has switched to AFNetworking. I suggest you do it too, ASAP. – try catch finally Jun 20 '14 at 14:09
0

Yes, you can download audio files and play downloaded files through AVAudioPlayer. I don't exactly know how to pause when downloading, but I have downloaded audios and played. You can download either from ASIHTTP Request or other.

You can follow these links.

  1. http://allseeing-i.com/ASIHTTPRequest/How-to-use#tracking_progress
  2. About download file using ASIHttpRequest
Community
  • 1
  • 1
Khalid Usman
  • 1,272
  • 1
  • 13
  • 32
  • Yes,Sure ASIHTTPRequest is best method, you should use this download your audio files, if problem came then please let me know, I will try to solve,insha Allah. – Khalid Usman Apr 20 '12 at 14:14