2010-08-21 76 views
0

所以应该在iOS 4 SDK中编辑并写入用户的iTunes资料库。我可以从我的iPhone/iPod库成功加载AVAsset,但作为一个快速测试,我试图用AVAssetExportSession立即覆盖相同的文件,但它总是返回状态“4”,我认为是AVAssetExportSessionStatusFailed。在文档,它说:如何使用AVAssetExportSession覆盖我的iPhone/iPod库中的歌曲?


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

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

谢谢!

+0

if(exportSession.status == AVAssetExportSessionStatusFailed)NSLog(@“failed”); if(exportSession.status == AVAssetExportSessionStatusCancelled)NSLog(@“canceled”); 你确定你被允许覆盖吗? – Thomas 2010-08-21 20:17:01

+0

也'NSLog(@“ExportSessionError:%@”,exportSession.error);'应该有所帮助。 – Thomas 2010-08-21 20:22:11

+0

谢谢,看起来它失败了。所以这是战斗的一半。另一半是找出原因! :)我想知道是否有一些方法可以将新文件保存到用户目录,然后将其添加到库中,而不是直接写入asset.URL url。嗯... – taber 2010-08-22 01:32:05

回答

2

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

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]; 
+0

感谢代码,抱歉我不是更清楚:我想将音频文件写入用户的音乐库。 – taber 2012-03-21 14:39:57