3

Can we store ipod music library file (song) to iPhone App folder? if Yes Please suggest How can I do that?

Please Help

Thanks

ios
  • 6,134
  • 20
  • 71
  • 103
  • 1
    In what way does this question differ from your last one? http://stackoverflow.com/questions/4743959/how-to-import-music-file-from-iphone-music-library-to-iphone-app – spender Jan 20 '11 at 11:12
  • I have used two ways too hold the song in data formate one i mention below – GhostRider Jan 20 '11 at 11:26
  • 1
    Chris Adamson has a blog entry that explains it [here](http://www.subfurther.com/blog/2010/12/13/from-ipod-library-to-pcm-samples-in-far-fewer-steps-than-were-previously-necessary/) – Aran Mulholland Jan 20 '11 at 11:11

2 Answers2

4

i show audio list on table view when i click on audio tableview then that song i hold in nsdata for upload

-(void)startVideoStart:(NSNumber*)number

{

    MPMediaItem *song = [miPodSongsArray objectAtIndex:[number intValue]];

    songTitle = [song valueForProperty: MPMediaItemPropertyTitle];

    NSURL *url = [song valueForProperty:MPMediaItemPropertyAssetURL];

    [NSThread detachNewThreadSelector:@selector(loadAudioInBackground:) toTarget:self withObject:url];

    [self performSelector:@selector(UploadSong:) withObject:songDict afterDelay:10];

}

-(void)loadAudioInBackground:(NSURL*)url

{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];


    NSLog(@"%@",url);

    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSLog (@"compatible presets for songAsset: %@",[AVAssetExportSession exportPresetsCompatibleWithAsset:songAsset]);

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
                                      initWithAsset: songAsset
                                      presetName: AVAssetExportPresetPassthrough];

    NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);

    exporter.outputFileType = @"com.apple.m4a-audio";

    NSString *exportFile = [documentsDirectory stringByAppendingPathComponent: @"item.m4a"];

    NSError *error1;

    if([[NSFileManager defaultManager] fileExistsAtPath:exportFile]) 
    {

        [[NSFileManager defaultManager] removeItemAtPath:exportFile error:&error1];

}

    NSURL* exportURL = [[NSURL fileURLWithPath:exportFile] retain];

    exporter.outputURL = exportURL; 

    // do the export
    [exporter exportAsynchronouslyWithCompletionHandler:^{

        NSData *data1 = [NSData dataWithContentsOfFile: [documentsDirectory stringByAppendingPathComponent: @"item.m4a"]];

        //NSLog(@"%@",data1);

        if (songDict) {

            [songDict release];

            songDict=nil;

        }

        songDict= [[NSMutableDictionary alloc]init];

        [songDict setValue:data1 forKey:@"AudioData"];



        [songDict setValue:songTitle forKey:@"AudioName"];

        [songDict setValue:[[mAppDelegate.userInfoArray objectAtIndex:1]valueForKey:@"user_id"] forKey:@"user_id"];

        //NSLog(@"%@",infoDict);

        mAppDelegate.uploadType = @"Audio";

        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");


                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;
}
        }

    }];

    [pool release];



}
Pankaj Kainthla
  • 2,439
  • 5
  • 31
  • 61
GhostRider
  • 1,197
  • 10
  • 19
  • What are these items? `miPodSongsArray` , `songTitle` and `songDict`? Do I need to add any framework for`AVAssetExportSession` and `AVURLAsset`? – ios Jan 20 '11 at 12:20
  • mipod songs array is array of songs that in ipod library , songdict hold the data to send to other class for parsing, songtitle is name of the song of ipod library. and you have to assetslibrary framwork – GhostRider Jan 20 '11 at 12:22
0

Please have a look at TSLibraryImport also.It handles AVAssetExportSession code for you.

cjd
  • 549
  • 3
  • 11