将视频加载到库/缓存中的视图

时间:2016-04-27 13:24:55

标签: ios objective-c caching video

对于我的应用程序我希望用户录制一个视频,该视频将存储在缓存目录中,以便在整个应用程序中重复使用。

NSString *DestFilename = @ "output.mov";

//Set the file save to URL
NSLog(@"Starting recording to file: %@", DestFilename);
NSString *DestPath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
DestPath = [paths objectAtIndex:0];
DestPath = [DestPath stringByAppendingPathComponent:DestFilename];

NSURL* saveLocationURL = [[NSURL alloc] initFileURLWithPath:DestPath];
[MovieFileOutput startRecordingToOutputFileURL:saveLocationURL recordingDelegate:self];

NSLog说:

url = file:///var/mobile/Containers/Data/Application/BD41ABB0-77BA-4872-B447-07906A3C6FA7/Library/Caches/output.mov

如何将此视频加载或可能嵌入我的故事板中的一个ViewControllers中?

1 个答案:

答案 0 :(得分:1)

基本上,您的视频网址就在那里,因此您可以将视频加载到视图控制器中并以两种可能的方式播放:使用MPMoviePlayerController或AVPlayer

例如来自您的视图控制器

    //The first approach
    self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:saveLocationURL];
    [self.moviePlayerController.view setFrame: CGRectMake(0, 0, 1024, 768)];
    [self.view addSubview:self.moviePlayerController.view];

    [self.moviePlayerController play];

    //The second approach
    self.playerItem = [AVPlayerItem playerItemWithURL:saveLocationURL];
    self.player = [AVPlayer playerWithPlayerItem:playerItem];

    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    layer.frame = CGRectMake(0, 0, 1024, 768);

    [self.view.layer addSublayer:layer];
    [self.player play];

希望这会对你有所帮助。