IOS 4.0的新功能列表表明,AV Foundation框架已经为媒体项目提供了媒体资产管理,跟踪管理,媒体编辑和元数据管理.这是什么意思?
我试图得到一些关于此的帮助/文档,但找不到任何东西..
谢谢,
托尼
Jac*_*rse 14
是的,你可以做你提到的大部分事情.我认为访问手机的媒体文件并不是那么简单.但是,如果您愿意,可以从网络中读取数据并将其导出到相机胶卷.
首先,您必须导入视频或音频文件.
您需要开始的是您自己的视频中创建的视频播放器.如果你不喜欢播放你的视频,只是简单地编写你的东西,你可以简单地去看看.
这很容易:1.创建一个可变组合:
AVMutableComposition *composition = [AVMutableComposition composition];
Run Code Online (Sandbox Code Playgroud)
这将保存您的视频.现在你有一个空的Composition-Asset.从您的目录或Web添加一些文件:
NSURL* sourceMovieURL = [NSURL fileURLWithPath:moviePath];
AVURLAsset* sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
Run Code Online (Sandbox Code Playgroud)
然后将您的视频添加到您的作品中
// calculate time
CMTimeRange editRange = CMTimeRangeMake(CMTimeMake(0, 600), CMTimeMake(sourceAsset.duration.value, sourceAsset.duration.timescale));
// and add into your composition
BOOL result = [composition insertTimeRange:editRange ofAsset:sourceAsset atTime:composition.duration error:&editError];
Run Code Online (Sandbox Code Playgroud)
如果您想在合成中添加更多视频,可以添加其他资源,然后使用您的时间范围将其重新设置到合成中.现在,您可以使用以下代码导出新的合成:
NSError *exportError = nil;
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = @"com.apple.quicktime-movie";
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch (exportSession.status) {
case AVAssetExportSessionStatusFailed:{
NSLog (@"FAIL");
[self performSelectorOnMainThread:@selector (doPostExportFailed:)
withObject:nil
waitUntilDone:NO];
break;
}
case AVAssetExportSessionStatusCompleted: {
NSLog (@"SUCCESS");
[self performSelectorOnMainThread:@selector (doPostExportSuccess:)
withObject:nil
waitUntilDone:NO];
break;
}
};
}];
Run Code Online (Sandbox Code Playgroud)
如果您想播放视频,请使用这样的代码(我假设您可以访问您的视图):
// create an AVPlayer with your composition
AVPlayer* mp = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:composition]];
// Add the player to your UserInterface
// Create a PlayerLayer:
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:mp];
// integrate it to your view. Here you can customize your player (Fullscreen, or a small preview)
[[self view].layer insertSublayer:playerLayer atIndex:0];
playerLayer.frame = [self view].layer.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
Run Code Online (Sandbox Code Playgroud)
最后播放你的视频:
[mp play];
Run Code Online (Sandbox Code Playgroud)
导出到相机胶卷:
NSString* exportVideoPath = >>your local path to your exported file<<
UISaveVideoAtPathToSavedPhotosAlbum (exportVideoPath, self, @selector(video:didFinishSavingWithError: contextInfo:), nil);
Run Code Online (Sandbox Code Playgroud)
并在完成后收到通知(您的回调方法)
- (void) video: (NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
// Error is nil, if succeeded
NSLog(@"Finished saving video with error: %@", error);
// do postprocessing here, i.e. notifications or UI stuff
Run Code Online (Sandbox Code Playgroud)
}
不幸的是,我还没有找到任何"合法"的解决方案来从相机中读取.
入门的一个非常好的来源是:
http://www.subfurther.com/blog/?cat=51
下载VTM_Player.zip,VTM_AVRecPlay.zip或VTM_AVEditor.zip以获得非常好的介绍