如何使用AVAssetExportSession覆盖iPhone / iPod库中的歌曲?

时间:2010-08-21 20:09:01

标签: iphone file-io ios4 iphone-4 mpmediaitemcollection

因此,据推测,在iOS 4 SDK中,您可以编辑并写入用户的iTunes资料库。我可以从我的iPhone / iPod库成功加载AVAsset,但作为一个快速测试,我试图使用AVAssetExportSession立即覆盖同一个文件,但它总是返回状态“4”我 THINK 是AVAssetExportSessionStatusFailed ...在文档中说:


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

但是在AVAssetExportSession.h中它说:


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

这是我正在使用的代码:



// 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];

所以无论如何......我猜它是“失败”或“取消”。有没有其他人成功写入媒体库?

谢谢!

2 个答案:

答案 0 :(得分:2)

你不能写入iTunes库,只能现在读取它。

答案 1 :(得分: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];