0

So supposedly in the iOS 4 SDK you can edit and write to the user's iTunes library. I can successfully load an AVAsset from my iPhone/iPod library, but as a quick test I'm trying to just overwrite the same file right away using AVAssetExportSession but it's always returning the status "4" which I THINK is AVAssetExportSessionStatusFailed... In the documentation it says:


enum {
    AVAssetExportSessionStatusUnknown,
    AVAssetExportSessionStatusExporting,
    AVAssetExportSessionStatusCompleted,
    AVAssetExportSessionStatusFailed,
    AVAssetExportSessionStatusCancelled,
    AVAssetExportSessionStatusWaiting
};

but in AVAssetExportSession.h it says:


enum {
    AVAssetExportSessionStatusUnknown,
    AVAssetExportSessionStatusWaiting,
    AVAssetExportSessionStatusExporting,
    AVAssetExportSessionStatusCompleted,
    AVAssetExportSessionStatusFailed,
    AVAssetExportSessionStatusCancelled
};
typedef NSInteger AVAssetExportSessionStatus;

Here's the code I'm using:



// before this, i'm using mpmediapicker to pick an m4a file i synched with my itunes library 

NSURL *assetUrl = [[self.userMediaItemCollection.items objectAtIndex: 0] valueForProperty: MPMediaItemPropertyAssetURL];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL: assetUrl options: nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset: asset presetName: AVAssetExportPresetAppleM4A];
exportSession.outputURL = asset.URL;
exportSession.outputFileType = AVFileTypeAppleM4A;

NSLog(@"output filetype: %@", exportSession.outputFileType);
// prints "com.apple.m4a-audio"

[exportSession exportAsynchronouslyWithCompletionHandler: ^(void) {
    NSLog(@"status: %i for %@", exportSession.status, exportSession.outputURL);
    // prints "status: 4 for ipod-library://item/item.m4a?id=3631988601206299774"
}];

[exportSession release];

So either way... I guess it's "failed" or "cancelled." Has anyone else successfully written to the media library before?

Thanks!

taber
  • 3,166
  • 4
  • 46
  • 72
  • if(exportSession.status == AVAssetExportSessionStatusFailed) NSLog(@"failed"); if(exportSession.status == AVAssetExportSessionStatusCancelled) NSLog(@"cancelled"); Are you sure that you are allowed to overwrite? – Thomas Aug 21 '10 at 20:17
  • Also `NSLog(@"ExportSessionError: %@", exportSession.error);` should help. – Thomas Aug 21 '10 at 20:22
  • thanks, looks like it's failing. so that's half the battle. the other half is finding out why! :) i wonder if there's some way that I can save out a new file to the user dir and then add it to the library instead of writing directly to the asset.URL url. hmm... – taber Aug 22 '10 at 01:32
  • what does `NSLog(@"ExportSessionError: %@", [exportSession.error localizedDescription])` say? – Thomas Aug 22 '10 at 19:48
  • ExportSessionError: The operation couldn’t be completed. (AVFoundationErrorDomain error -11800.) :( – taber Aug 23 '10 at 15:52
  • looks like a sandbox issue - i guess apple doesn't let you modify any ipod-library:// items. bummer. – taber Aug 23 '10 at 16:10
  • I think this [link][1] works for you whet you want... [1]: http://stackoverflow.com/questions/9802288/songs-imported-from-ipod-library-not-working-for-ios-5-1-but-works-fine-for-5-0/9806135#9806135 – Nilesh Kikani Mar 21 '12 at 14:03

2 Answers2

2

you cannot write to itunes library, only read from it now.

drunknbass
  • 1,662
  • 1
  • 13
  • 19
0
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSParameterAssert(library);
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:[NSURL     fileURLWithPath:movieFileName]]) {
   [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:movieFileName]     completionBlock:^(NSURL *assetURL, NSError *error){}];
}
[library release];
Orbitus007
  • 685
  • 6
  • 12
  • Thanks for the code, sorry I wasn't more clear: I'd like to write audio files to the user's music library. – taber Mar 21 '12 at 14:39