所以應該在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];
所以無論如何......我猜這是「失敗」或「取消」。有沒有其他人成功寫入媒體庫之前?
謝謝!
if(exportSession.status == AVAssetExportSessionStatusFailed)NSLog(@「failed」); if(exportSession.status == AVAssetExportSessionStatusCancelled)NSLog(@「canceled」); 你確定你被允許覆蓋嗎? – Thomas 2010-08-21 20:17:01
也'NSLog(@「ExportSessionError:%@」,exportSession.error);'應該有所幫助。 – Thomas 2010-08-21 20:22:11
謝謝,看起來它失敗了。所以這是戰鬥的一半。另一半是找出原因! :)我想知道是否有一些方法可以將新文件保存到用戶目錄,然後將其添加到庫中,而不是直接寫入asset.URL url。嗯... – taber 2010-08-22 01:32:05