【问题标题】:How can I use AVAssetExportSession to overwrite songs in my iPhone/iPod library?如何使用 AVAssetExportSession 覆盖 iPhone/iPod 库中的歌曲?
【发布时间】:2010-08-21 20:09:00
【问题描述】:

所以据说在 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];

所以无论哪种方式......我猜它是“失败”或“取消”。之前有其他人成功写入媒体库吗?

谢谢!

【问题讨论】:

  • if(exportSession.status == AVAssetExportSessionStatusFailed) NSLog(@"failed"); if(exportSession.status == AVAssetExportSessionStatusCancelled) NSLog(@"cancelled");您确定允许覆盖吗?
  • NSLog(@"ExportSessionError: %@", exportSession.error); 也应该有帮助。
  • 谢谢,看来它失败了。所以这是成功的一半。另一半在找出原因! :) 我想知道是否有某种方法可以将新文件保存到用户目录,然后将其添加到库中,而不是直接写入asset.URL url。嗯……
  • NSLog(@"ExportSessionError: %@", [exportSession.error localizedDescription]) 说什么?
  • ExportSessionError: 操作无法完成。 (AVFoundationErrorDomain 错误 -11800。):(

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


【解决方案1】:

您无法写入 iTunes 库,现在只能从中读取。

【讨论】:

    【解决方案2】:
    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];
    

    【讨论】:

    • 感谢您的代码,抱歉我不是更清楚:我想将音频文件写入用户的音乐库。
    猜你喜欢
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多