2010-07-21 50 views
5

IOS 4.0的新功能列表指出,AV Foundation框架已获得媒体项目的媒体资产管理,跟踪管理,媒体编辑和元数据管理。这是什么意思?AVAsset和AVAssetTrack - IOS 4.0中的跟踪管理

  1. 使用音轨管理和媒体资产管理可以从照片应用程序访问媒体文件吗?
  2. 我可以使用AVComposition和导出将我的自定义作品发送到服务器吗?
  3. 我可以重命名,移动,编辑资产的元数据信息吗?

我试图让一些这方面的帮助/文档,但没有找到任何东西..

感谢,

托尼

回答

14

是的,你可以做大部分的东西你提及。 我认为访问手机的媒体文件并不那么简单。但是,如果你喜欢,你可以从网络上读取数据并将其导出到相机胶卷上。

首先你必须导入你的视频或音频文件。

您需要开始的是您自己的视频播放器,您可以在自己的视图中创建。 如果你不喜欢播放你的视频,但只是简单地编写你的东西,你可以简单地去看看。

这是很容易的: 1.创建一个可变组成:

AVMutableComposition *composition = [AVMutableComposition composition]; 

这将举行您的视频。现在你有一个空的组合资产。 从目录或从网站添加一些文件:

NSURL* sourceMovieURL = [NSURL fileURLWithPath:moviePath]; 
AVURLAsset* sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil]; 

您的视频,然后添加到您的组成

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

如果你想添加更多的视频到您的作品,您可以添加其他资产并使用您的时间范围将其重新设置到您的作品中。 现在你可以出口的新的组合物中使用这样的代码:

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

如果要播放视频,使用这样的代码(我假设,您可以访问你的看法):

// 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; 

最后播放视频:

[mp play]; 

导出到相机胶卷:

NSString* exportVideoPath = >>your local path to your exported file<< 
UISaveVideoAtPathToSavedPhotosAlbum (exportVideoPath, self, @selector(video:didFinishSavingWithError: contextInfo:), nil); 

,如果它完成得到通知(你的回调方法)

- (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 

}

不幸的是我还没有发现任何“合法”的解决方案从cameraroll阅读。

一个入门的很好的来源是:

http://www.subfurther.com/blog/?cat=51

下载VTM_Player.zip,VTM_AVRecPlay.zip或VTM_AVEditor.zip了很不错的引入这一

+0

谢谢!好答案! – SAKrisT 2011-12-15 16:20:54

+0

嗨你的代码不显示视频...可能是什么问题? – 2012-05-03 08:10:46

+0

@iError:嗯,这不容易回答,因为路上有很多障碍。我发现,电影播放器​​叠加有时会旋转,这意味着它可以在那里,但在可见视图框外。如果您正在使用弧进行开发,请确保您有一个强大的参考或对电影播放器​​的属性参考。 ARC可能会在离开创建方法后立即释放玩家对象。检查是否启用了自动旋转。也许你需要使用“layer.transform”属性来设置你的旋转。 – JackPearse 2012-05-08 12:04:41