AVPlayer不会播放MovieFile

时间:2015-10-20 16:43:30

标签: ios avfoundation avplayer avcapturemoviefileoutput

我使用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];

}

1 个答案:

答案 0 :(得分:0)

当我删除该行时:

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

它有效。