簡體   English   中英

某些文件的AVAssetExport失敗

[英]AVAssetExport fails for some files

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

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:Error Domain = AVFoundationErrorDomain Code = -11800“操作無法完成”UserInfo = 0x214f20 {NSLocalizedFailureReason =發生未知錯誤(-12124),NSUnderlyingError = 0x218270“操作無法完成。(OSStatus error -12124 。)“,NSLocalizedDescription =操作無法完成}

我自己玩了很多,一些答案可能是相關和正確的。 另外需要注意的是,如果嘗試組合具有不同幀速率的視頻,我經常會遇到類似的AVFoundation錯誤。 檢查源文件的幀速率並檢查CMTime的幀速率。 在將某些文件添加到AVMutableComposition之前,您可能需要預先處理它們。

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

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

    return NO;
}

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

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

您正在使用的音樂文件路徑可能有誤。 試試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM