Loading

Wednesday, September 7, 2011

Upload Song from iPod Library to Webserver using ASIHTTPRequest

1. in Your .h file

     #import
    and use include MPMediaPickerControllerDelegate


2. in .m file where your brows button
     MPMediaPickerController *picker =[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAnyAudio];

picker.delegate = self;
        picker.allowsPickingMultipleItems = YES;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque animated:YES];
      [self presentModalViewController:picker animated:YES];
        [picker release];

3.  #pragma mark -

#pragma mark Media Picker Delegate Methods

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) theCollection {   

NSArray *ArrItems=theCollection.items;
MPMediaItem *curItem = [ArrItems objectAtIndex:0]; 
NSString *SongName=[curItem valueForProperty:MPMediaItemPropertyTitle];

NSURL *assetURL = [curItem valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                           initWithAsset:songAsset
                                           presetName:AVAssetExportPresetPassthrough];
NSArray *tracks = [songAsset tracksWithMediaType:AVMediaTypeAudio];
AVAssetTrack *track = [tracks objectAtIndex:0];
id desc = [track.formatDescriptions objectAtIndex:0];
const AudioStreamBasicDescription *audioDesc = CMAudioFormatDescriptionGetStreamBasicDescription((CMAudioFormatDescriptionRef)desc);
    FourCharCode formatID = audioDesc->mFormatID;
NSString *fileType = nil;
NSString *ex = nil;
switch (formatID) {
            
        case kAudioFormatLinearPCM:
        {
            UInt32 flags = audioDesc->mFormatFlags;
            if (flags & kAudioFormatFlagIsBigEndian) {
                fileType = @"public.aiff-audio";
                ex = @"aif";
            } else {
                fileType = @"com.microsoft.waveform-audio";
                ex = @"wav";
            }
        }
            break;
            
        case kAudioFormatMPEGLayer3:
            fileType = @"com.apple.quicktime-movie";
            ex = @"mp3";
break;
            
        case kAudioFormatMPEG4AAC:
            fileType = @"com.apple.m4a-audio";
            ex = @"m4a";
            break;
            
        case kAudioFormatAppleLossless:
            fileType = @"com.apple.m4a-audio";
            ex = @"m4a";
            break;
            
        default:
            break;
    }
exportSession.outputFileType = fileType;
NSString *exportFile = [[self myDocumentsDirectory] stringByAppendingPathComponent:           
[NSString stringWithFormat:@"%@.%@",SongName,ex]];
NSURL *exportURL = [[NSURL fileURLWithPath:exportFile] retain];
exportSession.outputURL = exportURL; 
// do the export
// (completion handler block omitted) 
[exportSession exportAsynchronouslyWithCompletionHandler:
^{
NSData *Songdata = [NSData dataWithContentsOfFile: [[self myDocumentsDirectory
stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.%@",SongName,ex]]];
 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:uploadSong]];
[request setDelegate:self];
NSString *UserID = [[NSUserDefaults standardUserDefaults]objectForKey:@"USERID"];
 
[request setPostValue:@"23" forKey:@"user_id"];
[request setPostValue:@"1" forKey:@"category_id"];
[request addData:Songdata withFileName:SongName andContentType:ex forKey:@"upload_song"];
[request startAsynchronous];
 
}];


[self dismissModalViewControllerAnimated: YES];
}
-(NSString *)myDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}

4.

#pragma mark 
#pragma mark ASIHTTPRequestDelegate Methods
- (void)requestFinished:(ASIHTTPRequest *)request 
{
NSString *receivedString = [request responseString];
NSLog(@"%@",receivedString);
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSString *receivedString = [request responseString];
NSLog(@"%@",receivedString);
}
- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
{
NSString* str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}