Michael Castro
Michael Castro

Reputation: 314

Playing a video recorded with AVFoundation

I would like to use the same button to start and stop recording. I would like to use another button to play back the recording. Here is what I have:

- (IBAction)recordVideo:(id)sender {
    if(!self.movieOutput.isRecording) {

    NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];

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

    [self.movieOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputPath]
                                  recordingDelegate:self];

    Float64 maximumVideoLength = 5; //Whatever value you wish to set as the maximum, in seconds
    int32_t prefferedTimeScale = 30; //Frames per second

    CMTime maxDuration = CMTimeMakeWithSeconds(maximumVideoLength, prefferedTimeScale);

    self.movieFileOutput.maxRecordedDuration = maxDuration;
    self.movieFileOutput.minFreeDiskSpaceLimit = 1024*1024;

}
else
{
    [self.movieOutput stopRecording];
}

- (void) captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
  fromConnections:(NSArray *)connections error:(NSError *)error
{
 NSLog(@"Recording to file ended");

[_captureSession stopRunning];
}

Then to play:

- (IBAction)playVideo:(id)sender {
NSURL *fileURL = [NSURL URLWithString:@"outputPath"];
self.avPlayer = [AVPlayer playerWithURL:fileURL];

AVPlayerLayer *movieLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

movieLayer.frame = self.cameraView.bounds;
movieLayer.videoGravity = AVLayerVideoGravityResize;
[self.cameraView.layer addSublayer:movieLayer];
[_avPlayer play];

When I run and press the playback button I get no errors and I see no avplayer.

Upvotes: 0

Views: 490

Answers (2)

Bhoomi
Bhoomi

Reputation: 81

You are recording and saving file in temporary directory

NSString *outputPath = [NSTemporaryDirectory()stringByAppendingPathComponent:@"output.mp4"];

and trying to play from bundle path.Use the same path to play recording also.

  1. First, check Is your video is recorded and saved properly or not.From your code, the video is saved Temporary directory.Check the video at the Path.If it is exist or not.

    NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];
    NSLog(@"%@", outputPath);
    
  2. In your code, you are trying to play video from outPutPath, which is not defined and initialize in your code.If you have defined outPutPath as property or variable, then you need to initialise _outPutPath, with the same path you save the video.

    NSString *outputPath = [NSTemporaryDirectory()stringByAppendingPathComponent:@"output.mp4"];
    _outputPath = outputPath;
    
  3. To Play Video Try this,

    if ([[NSFileManager defaultManager]fileExistsAtPath: _ouputPath]) {
    
        AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:_ouputPath]];
        _avPlayer = [[AVPlayer alloc]initWithPlayerItem:[[AVPlayerItem alloc]initWithAsset:asset]];
    
        AVPlayerLayer *movieLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
    movieLayer.frame = self.cameraView.bounds;
        [self.cameraView.layer addSublayer:movieLayer];
        [self.avPlayer play]; 
    }
    

Upvotes: 2

Ravi Parsania
Ravi Parsania

Reputation: 715

Replace your this line :

NSURL *url = [NSURL fileURLWithPath:filePath];

with this: 

NSURL *url=[NSURL URLWithString:filePath];

 & then try.

Upvotes: 0

Related Questions