I'm trying to record and play movies with my app using qtkit. I record the video in one view and display it into another view. Here's how I do it
- (void)startRecording
{
NSString *applicationSupportDirectory = [[NSFileManager defaultManager] applicationSupportDirectory];
NSString *path = [applicationSupportDirectory stringByAppendingPathComponent:kVideoOutputName];
NSURL *url = [NSURL fileURLWithPath:path];
// Delete the previous file
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
mCaptureMovieFileOutput.delegate = self;
[mCaptureMovieFileOutput recordToOutputFileURL:url];
}
- (void)stopRecording
{
[mCaptureMovieFileOutput recordToOutputFileURL:nil];
}
- (void)captureOutput:(QTCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL forConnections:(NSArray *)connections dueToError:(NSError *)error
{
// [[NSWorkspace sharedWorkspace] openURL:outputFileURL];
// removes the current view
[self cleanView];
MyViewController *controller = [[SharingViewController alloc] init];
controllerpath.path = outputFileURL;
[self.view addSubview:[controller view]];
[self stopCamera];
}
Now in my view controller I assign the movie to my movie player.
- (void)awakeFromNib
{
NSError *error;
moviePlayer.movie = [QTMovie movieWithURL:path error:&error];
NSLog(@"%@", [error localizedDescription]);
}
Now, this code works the first time around, but I need to register and show multiple times.
There's already one problem here. If I want to record videos multiple times, I have to delete the first one, otherwise after the first time it wont record anything (it complains that a file already exists).
The problem is that after the first time, it also doesn't show the video at all. When I execute [QTMovie movieWithURL:path error:&error]; it complains that the file or directory does not exists, when in reality it does (I also checked with [QTMovie canInitWithUrl:]).
I'm not sure what's going on here. The apple's sample code is able to record multiple times, but for some reasons I can't without first deleting the existing file (it works the first time, though).
开发者_如何学GoI'd be happy to provide further details if needed.
EDIT: If I use a different name every time for the video, everything works. So this is really an issue about recording with the same name every time.
I ended up using a different file name for every file.
I ran into the same issue, and I found that setting the movie view to nil, prior to reusing the same file name resolved the issue.
I get the same weird problem creating a QTMovie. I just loaded the file to an NSData object like this and it worked:
[self setMovieData:[NSData dataWithContentsOfURL:[self movieURL]]];
[self setMovie:[QTMovie movieWithData:[self movieData] error:&error]];
精彩评论