无法将mp4 / m4a写入文件

时间:2014-12-18 00:37:59

标签: objective-c xcode file url mp4

我正在尝试将mp4 / m4a写入文件,以便我可以编辑它的元数据。它虽然没有保存到文件中。我不知道为什么它没有保存,而且我一直试图修复它很长一段时间。 _previewData包含来自网址(www.example.com/file.m4a)的数据,并且检索成功。

_previewData = [NSMutableData dataWithContentsOfURL:[NSURL URLWithString:@"http://a281.phobos.apple.com/us/r1000/119/Music/v4/f1/7b/d6/f17b‌​d6e3-55c0-b7e0-9863-bc522900e950/mzaf_5153970109972844579.aac.m4a"]];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentDirectory=[paths objectAtIndex:0];
                NSString *path = [documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat: @"file.mp4"]];
                NSFileManager *filemanager;
                filemanager = [NSFileManager defaultManager];
                [_previewData writeToFile:path atomically:YES];
                if ([filemanager fileExistsAtPath:path]) {
                    // Runs
                    NSLog(@"It worked");
                }
                NSLog(@"%@",path);

使用这个新代码,它将其保存在一个文件中,上面的NSLog写入/var/mobile/Containers/Data/Application/FF533DE5-1A07-44A9-A191-42F3301BB150/Documents/file.mp4。然后我使用以下代码编辑文件:

ANMovie* file = [[ANMovie alloc] initWithFile:path]; // or .mp4
                NSData* jpegCover = UIImageJPEGRepresentation(artworkImage, 1.0);
                _imageView.image = [UIImage imageWithData:jpegCover];
                ANMetadata* metadata = [[ANMetadata alloc] init];
                metadata.albumCover = [[ANMetadataImage alloc] initWithImageData:jpegCover type:ANMetadataImageTypeJPG];
                [file setMovieMetadata:metadata];
                [file close];

                _previewData = [NSMutableData dataWithContentsOfFile:path];

mp4 / m4a虽然是空的,但无法在某处成功复制和粘贴。 这是用于复制mp4和其他数据的代码:

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
                NSDictionary *imageItem=@{@"public.mpeg-4-audio":self.previewData};
                NSDictionary *textItem=@{@"public.plain-text":self.linkData};

                pasteboard.items=@[imageItem,textItem];

1 个答案:

答案 0 :(得分:0)

你的代码一定有问题,你可以这样做,而且它对我来说很好:

NSString *path = [[NSBundle mainBundle] pathForResource:@"IMG_0387" ofType:@"MOV"];
NSData *netData = [NSData dataWithContentsOfFile:path];

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentPath = paths.firstObject;
NSString *savePath = [documentPath stringByAppendingPathComponent:@"tmp.MOV"];
[netData writeToFile:savePath atomically:YES];
NSFileManager *manager = [NSFileManager defaultManager];
BOOL saved = [manager fileExistsAtPath:savePath isDirectory:NULL];
if (saved) {
    NSLog(@"it's saved");
}

'IMG_0387.MOV'是mainBundle中的mov文件。