Friday, May 23, 2014

Import music and save it to Document Directory in ios

Here we will select music from music album and save it to Document Directory.

Let Start

1. Create Project.

2. Take button and initialize it.

3. Import MediaPlayer.framework.


#import <MediaPlayer/MediaPlayer.h>

Before that first add Framework.

4. On button click open media album to select music.

- (IBAction)btnMedia_clicked:(id)sender {
   
    MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
    
    mediaPicker.delegate = self;
    mediaPicker.allowsPickingMultipleItems = NO;
    mediaPicker.prompt = @"Select songs to play";
   //  UINavigationController *navi=[[UINavigationController alloc] initWithRootViewController:mediaPicker];
    [self presentViewController:mediaPicker animated:YES completion:Nil];
    [mediaPicker release];
    

}


5. on Media player Delegate method, import music and save it to Document Directory

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
    [self dismissViewControllerAnimated:YES completion:nil];
    if (mediaItemCollection) {
        
       MPMediaItem *song=[[[mediaItemCollection items] objectAtIndex:0] retain];
        if (! song) {
            return;
        }
        NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
        AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
        
        NSLog (@"Core Audio %@ directly open library URL %@",
               coreAudioCanOpenURL (assetURL) ? @"can" : @"cannot",
               assetURL);
        
        NSLog (@"compatible presets for songAsset: %@",
               [AVAssetExportSession exportPresetsCompatibleWithAsset:songAsset]);
        
        
        /* approach 1: export just the song itself
         */
        AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
                                          initWithAsset: songAsset
                                          presetName: AVAssetExportPresetAppleM4A];
        NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
        exporter.outputFileType = @"com.apple.m4a-audio";
        // exporter.outputFileType=@"com.apple.quicktime-movie";
        NSString *exportFile = [myDocumentsDirectory() stringByAppendingPathComponent: @"exported.m4a"];
        
        myDeleteFile(exportFile);
        NSURL *exportURL = [[NSURL fileURLWithPath:exportFile] retain] ;
        exporter.outputURL = exportURL;
        
        // do the export
        [exporter exportAsynchronouslyWithCompletionHandler:^{
            int exportStatus = exporter.status;
            switch (exportStatus) {
                case AVAssetExportSessionStatusFailed: {
                    // log error to text view
                    NSError *exportError = exporter.error;
                    NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
                    //errorView.text = exportError ? [exportError description] : @"Unknown failure";
                   // errorView.hidden = NO;
                    break;
                }
                case AVAssetExportSessionStatusCompleted: {
                    NSLog (@"AVAssetExportSessionStatusCompleted");
                   // fileNameLabel.text = [exporter.outputURL lastPathComponent];
                    // set up AVPlayer
                    //[self setUpAVPlayerForURL: exporter.outputURL];
                   // [self enablePCMConversionIfCoreAudioCanOpenURL: exporter.outputURL];
                    break;
                }
                case AVAssetExportSessionStatusUnknown: { NSLog (@"AVAssetExportSessionStatusUnknown"); break;}
                case AVAssetExportSessionStatusExporting: { NSLog (@"AVAssetExportSessionStatusExporting"); break;}
                case AVAssetExportSessionStatusCancelled: { NSLog (@"AVAssetExportSessionStatusCancelled"); break;}
                case AVAssetExportSessionStatusWaiting: { NSLog (@"AVAssetExportSessionStatusWaiting"); break;}
                default: { NSLog (@"didn't get export status"); break;}
            }
        }];

    }
    
}
-(void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
  [self dismissViewControllerAnimated:YES completion:nil];

}


IT will import song and put it on document directory






Thursday, May 22, 2014

Manage Two Tableview in ios Xcode

On click UITableview cell Effect other UITableview cell

Example. when one click on 6th row of one tableview cell move cell of Second tableview to 60th row.

It like   1 -> 10,2 -> 20,3 -> 30 etc.

Now continue with code.

1. Take two Tableview side by side on viewController.

2. Initialize it.



3. Set Delegate and Datasource to fileOwner.

 4. on select row set scroll position of second tableview to particular cell.


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView==self.tblTen)
        return 10;
    else if(tableView==self.tblTenList)
        return 100;
   
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellStr=@"clsten";
    ClsTen *cellTen=(ClsTen *)[tableView dequeueReusableCellWithIdentifier:cellStr];
                     if(cellTen==nil)
                     {
                         NSArray *nib=nil;
                          nib=[[NSBundle mainBundle] loadNibNamed:@"clsTen" owner:self options:nil];
                         cellTen=[nib objectAtIndex:0];
                         cellTen.backgroundColor=[UIColor clearColor];
                       
                     }
 
    cellTen.lblNo.text=[NSString stringWithFormat:@"%ld",indexPath.row];
    return cellTen;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView==self.tblTen)
   [[self tblTenList] scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row * 10 inSection:0]  atScrollPosition:UITableViewScrollPositionTop animated:YES];
    if(tableView==self.tblTenList)
    {
        TenSubList *tenSubList=nil;
        tenSubList=[[TenSubList alloc] initWithNibName:@"TenSubList_iPhone" bundle:nil];
       
        [self.navigationController pushViewController:tenSubList animated:YES];
    }
}








Result























When Click on 3rd cell, second cell move to 30