2015-10-20 102 views
0

我使用AVCaptureMovieFileOutput来捕捉视频文件。AVPlayer不会播放MovieFile

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

} 
+0

好的。所以当你在播放它之前等待文件完成下面方法的加载时,它会起作用吗? – rocky

+0

@rocky我明白了,谢谢,并在下面回答。但是,我知道你想检查并删除现有文件。有什么建议么 – Peter

回答

0

当我删除行:

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

它的工作原理。