Search code examples
iosiphoneios7

downloading audio using ASIHTTPRequest


I am downloading audio using ASIHTTPRequest, when I check destination folder, there is no audio file there. Here is sample code.

naking destination Path in viewdidLoad

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

Here is the Download audio function using ASIHTTPREQUEST and making Queue :**

- (IBAction)DownloadOneFile:(UIButton *)sender {
if (!networkQueue) {
    networkQueue = [[ASINetworkQueue alloc] init];
}
[networkQueue reset];
networkQueue.maxConcurrentOperationCount=1;
[networkQueue setDownloadProgressDelegate:_ProgressView];
networkQueue.shouldCancelAllRequestsOnFailure=NO;
[networkQueue setRequestDidFinishSelector:@selector(AudioFetchComplete:)];
[networkQueue setRequestDidFailSelector:@selector(AudioFetchFailed:)];
[networkQueue setShowAccurateProgress:YES];
[networkQueue setDelegate:self];

ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://appstabs.com/wahab_audios_files/1.mp3"]];[request setDownloadDestinationPath:[NSString stringWithFormat:@"%@",dataPath]];
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@-part",dataPath]];
request.shouldContinueWhenAppEntersBackground=YES;
[request setShowAccurateProgress:YES];
request.allowResumeForFileDownloads=YES;
[networkQueue addOperation:request];
[networkQueue go];
timer = [NSTimer scheduledTimerWithTimeInterval: 0.0
                                              target: self
                                            selector:@selector(onTick)
                                            userInfo: nil repeats:YES];

} - (void)AudioFetchComplete:(ASIHTTPRequest *)request{

NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@/",dataPath] ofType:@"mp3"];

NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error: &error];
audioPlayer.delegate=self;
audioPlayer.volume = 1;
[audioPlayer prepareToPlay];

}

But i am getting nil in "path" string and and my Audio player is not intialized.


Solution

  • You are trying to save in "Rabana" subdirectory but you not created subdirectory

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    dataPath = [documentsDirectory stringByAppendingPathComponent:@"Rabana"];
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
    

    And In place of this:

    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@/",dataPath] ofType:@"mp3"];
    

    ues this:

    NSString *path =    [dataPath stringByAppendingPathComponent:YOUR_FILE_NAME];