0

AVCaptureMovieFileOutput用来捕捉视频文件。

- (void)takeVideoAction:(id)sender {
    NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];

    NSFileManager *manager = [[NSFileManager alloc] init];
    if ([manager fileExistsAtPath:outputPath]){
        [manager removeItemAtPath:outputPath error:nil];
    }
   [movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputPath] recordingDelegate:self];
}   

然后尝试用 AVPlayer 播放它,但没有任何反应。从我一直在阅读的内容来看,我认为我需要等待文件以下面的方法完成加载,而不是立即尝试播放。

 - (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{

    AVAsset *asset = [AVAsset assetWithURL:outputFileURL];
    self.playerItem = [[AVPlayerItem alloc]initWithAsset:asset];

    self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    [[[self view] layer] insertSublayer:self.playerLayer atIndex:(int)self.view.layer.sublayers.count];
    [self.playerLayer setFrame:self.view.frame];
    //self.playerLayer.backgroundColor = [UIColor yellowColor].CGColor;
    [self.player play];

}
4

1 回答 1

0

当我删除该行时:

NSFileManager *manager = [[NSFileManager alloc] init];
if ([manager fileExistsAtPath:outputPath]){
    [manager removeItemAtPath:outputPath error:nil];
}

有用。

于 2015-10-21T22:28:38.047 回答