Reputation: 241
I have an iphone application which plays audio files (mp3) from a server, I want to add a download button so the user can download these files into the app. So he/she will not need the Internet connection each time they want to listen to these files. Does any one has a sample code for that ?
I saw many things on the web but I'm still confusion. Here is the code I'm using :
in the .h File :
#import
#import "MBProgressHUD.h"
#import
#import
@interface myViewController : UIViewController {
MBProgressHUD *HUD;
long long expectedLength;
long long currentLength;
AVAudioPlayer *theAudio;
}
-(NSString *)pathOfFile;
-(void)applicationWillTerminate:(NSNotification*)notification;
-(IBAction)play:(id)sender;
-(IBAction)download(id)sender;
@property (nonatomic,retain) AVAudioPlayer *theAudio;
in the .m File:
- (IBAction)download:(id)sender {
NSURL *URL = [NSURL URLWithString:@"http://www.server.com/myfile.mp3"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[connection release];
HUD = [[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] retain];
HUD.labelText = @"Loading...";
}
-(IBAction)play:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [NSString stringWithFormat:@"%@/song.mp3", documentsDirectory];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:file] error:nil];
[theAudio play];
}
-(NSString *)pathOfFile {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingFormat:@"myfile.plist"];
}
-(void)applicationWillTerminate:(NSNotification*)notification {
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:theAudio];
[array writeToFile:[self pathOfFile] atomically:YES];
[array release];
}
I don't know what is the problem or what I'm missing over here , can you please help me with more details ? Thanks in advance.
Upvotes: 2
Views: 6783
Reputation: 1154
Try below code:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
[request setDownloadDestinationPath:destinationPath];
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@-part",destinationPath]];
[request setDelegate:self];
[request setAllowResumeForFileDownloads:YES];
[request startAsynchronous];
And to check downloads:
- (void)requestStarted:(ASIHTTPRequest *)request;
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders;
- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL;
- (void)requestFinished:(ASIHTTPRequest *)request;
- (void)requestFailed:(ASIHTTPRequest *)request;
Above code will create a file "your_file.mp3-part" in destination folder while download in progress and "your_file.mp3" when download completes.
Hope it helps. Let me know if any doubts.
Upvotes: 0
Reputation: 1282
Use ASIHTTP for downloading, it asynchronous and more reliable. http://allseeing-i.com/ASIHTTPRequest/ This is the method through which you can download audios/videos. Pass parameter to it and start download?
Parameters: (1) File Path , Document Directory path where you want to save (2) Trimmed String , which is the lecture URL
+ (void)downloadingLecture:(NSString *)filePath withLectureUrl:(NSString *)trimmedString
{
AppDelegate *_appDel=(AppDelegate *)[[UIApplication sharedApplication] delegate];
[_appDel.queue setDelegate:self];
[_appDel.queue setMaxConcurrentOperationCount:10];
[_appDel.queue setShouldCancelAllRequestsOnFailure:NO];
[_appDel.queue setShowAccurateProgress:YES];
ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:trimmedString]];
[request setDelegate:self];
[request setDownloadDestinationPath:filePath];
[request setShowAccurateProgress:YES];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setUsername:downloadedSuccessfullString];
[request setDidFinishSelector:@selector(downloadedSuccessfully:)];
[request setDidFailSelector:@selector(downloadedFailure:)];
[_appDel.queue addOperation:request];
[_appDel.queue go];
}
Hope this will help you. If still issue then email me, I am sure I will do it, because I have implemented it in my two applications.
Upvotes: 2