-(void)startVideoStart:(int)number withSongArray:(NSArray *)miPodSongsArray
{
MPMediaItem *song = [miPodSongsArray objectAtIndex:number];
// id 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: AVAssetExportPresetAppleM4A];
NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
exporter.outputFileType = AVFileTypeAppleM4A;
NSString *exportFile = [documentsDirectory stringByAppendingPathComponent: @"item.m4a"];
NSURL* exportURL = [[NSURL fileURLWithPath:exportFile] retain];
exporter.outputURL = exportURL;
AVAssetWriter *aw = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:exportFile] fileType:AVFileTypeCoreAudioFormat error:nil];
NSError *error1;
if([[NSFileManager defaultManager] fileExistsAtPath:exportFile]){
[[NSFileManager defaultManager] removeItemAtPath:exportFile error:&error1];
}
// do the export
[exporter exportAsynchronouslyWithCompletionHandler:^{
NSData *data1 = [NSData dataWithContentsOfFile: [documentsDirectory stringByAppendingPathComponent: @"item.m4a"]];
int exportStatus = exporter.status;
switch (exportStatus) {
case AVAssetExportSessionStatusFailed: {
// log error to text view
NSError *exportError = exporter.error;
NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
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];
}