2012-07-03 95 views
16

我試圖導出iPod-Library中的音頻文件。我的目標是使用此iPod-Library文件在應用程序「文檔」文件夾中創建新文件。它無法僅爲某些項目創建文件。以下是我的代碼片段。某些文件的AVAsset導出失敗

AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] 
            initWithAsset: songAsset 
            presetName: AVAssetExportPresetAppleM4A]; 

exporter.outputFileType = @"com.apple.m4a-audio"; 

NSString *songName = [filename stringByAppendingString:@".m4a"]; 

NSString *musicfilepath = [documentsDirectory stringByAppendingPathComponent:@"musics/"]; 

[[NSFileManager defaultManager] createDirectoryAtPath:musicfilepath withIntermediateDirectories:YES attributes:nil error:nil]; 

NSString *exportFile = [musicfilepath stringByAppendingPathComponent:songName]; 



NSError *error1; 

if([[NSFileManager defaultManager] fileExistsAtPath:exportFile]) 
{ 

    [[NSFileManager defaultManager] removeItemAtPath:exportFile error:&error1]; 

} 

NSURL* exportURL = [[NSURL fileURLWithPath:exportFile] retain]; 

exporter.outputURL = exportURL; 

正在逐漸錯誤如圖所示當與錯誤處理程序塊試圖下面:

[exporter exportAsynchronouslyWithCompletionHandler:^{ 

    int exportStatus = exporter.status; 

    switch (exportStatus) { 

     case AVAssetExportSessionStatusFailed: { 

      NSError *exportError = exporter.error; 

      NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError); 

      break; 
     } 
    } 
}]; 

AVAssetExportSessionStatusFailed:錯誤域= AVFoundationErrorDomain代碼= -11800 「該操作不能完成」 的UserInfo = 0x214f20 {NSLocalizedFailureReason =發生未知錯誤(-12124),NSUnderlyingError = 0x218270「操作無法完成(OSStatus錯誤-12124。)」,NSLocalizedDescription =操作無法完成}

+0

設備上的文件是否有足夠的空間?也他們已經存在,你試圖導出他們也許? –

回答

0

如果您的iTunes匹配並且文件未下載到設備,則可能發生此情況。你可以檢查一下,看看你是否屬於這種情況?如果是這樣,我可以提供代碼來檢測它。

+ (BOOL)isDownloadedFromiCloud:(MPMediaItem *)item { 
    if ([item valueForProperty:MPMediaItemPropertyAssetURL] != nil) { 
     AVURLAsset *asset = [AVURLAsset assetWithURL:[item valueForProperty:MPMediaItemPropertyAssetURL]]; 
     if (asset.exportable) 
      return YES; 
    } 

    return NO; 
} 
0

我也經歷過這個。你使用iTunes匹配嗎?這可能是該文件不在實際設備上。您可以通過檢查屬性(如hasProtectedContent或確保exportableYES)在AVAsset上進行檢查。

此外,我注意到您正在使用AVExportSession預設AVAssetExportPresetAppleM4A'. This will re-encode the file you are exporting. It may be easier (and much faster) to just pass-though instead using AVAssetExportPresetPassthrough`。這假設你對現有的文件格式感到滿意,這可能是.m4a,.mp3,.wav等等。

0

你正在使用的音樂文件路徑可能是錯誤的。試試這個

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 

NSUserDomainMask, YES); 
NSString* documentsDirectory = [paths objectAtIndex:0]; 
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Music"]; 
NSError* error; 
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory    withIntermediateDirectories:NO attributes:nil error:&error]; 

NSString *exportFile = [documentsDirectory stringByAppendingPathComponent:songName]; 

if([[NSFileManager defaultManager] fileExistsAtPath:exportFile]) 
{ 

    [[NSFileManager defaultManager] removeItemAtPath:exportFile error:&error1]; 

} 

if (error != nil) { 
NSURL* exportURL = [[NSURL fileURLWithPath:exportFile] retain]; 

exporter.outputURL = exportURL; 
[exporter exportAsynchronouslyWithCompletionHandler:^{ 

int exportStatus = exporter.status; 

switch (exportStatus) { 

    case AVAssetExportSessionStatusFailed: { 

     NSError *exportError = exporter.error; 

     NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError); 

     break; 
     } 
    } 
    } 
]; 
} 
2

在與我一起玩了很多之後,其中一些答案可能是相關且正確的。另外需要注意的是,如果嘗試合併具有不同幀率的視頻,我經常會遇到類似的AVFoundation錯誤。檢查源文件的幀速率,並檢查您的CMTime也是如此。在將其添加到AVMutableComposition之前,您可能必須預先處理一些文件。