2015-10-20 106 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]; 
} 

它的工作原理。