播放使用AVFoundation录制的视频

时间:2015-12-08 17:24:17

标签: ios objective-c avfoundation avplayer

我想使用相同的按钮来开始和停止录制。我想用另一个按钮来播放录音。这就是我所拥有的:

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

然后玩:

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

当我跑步并按下播放按钮时,我没有错误,我看不到任何移动设备。

2 个答案:

答案 0 :(得分:2)

您正在临时目录中记录并保存文件

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

并尝试从捆绑路径播放。使用相同的路径播放录制内容。

  1. 首先,检查您的视频是否被正确记录和保存。从您的代码中,视频被保存为临时目录。在路径上检查视频。如果它存在与否。

    NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];
    NSLog(@"%@", outputPath);
    
  2. 在您的代码中,您尝试从outPutPath播放视频,该视频未在您的代码中定义和初始化。如果您已将outPutPath定义为属性或变量,则需要使用相同的路径初始化_outPutPath你保存视频。

    NSString *outputPath = [NSTemporaryDirectory()stringByAppendingPathComponent:@"output.mp4"];
    _outputPath = outputPath;
    
  3. 播放视频试试这个,

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

答案 1 :(得分:0)

替换你的这一行:

NSURL *url = [NSURL fileURLWithPath:filePath];

用这个:

NSURL *url=[NSURL URLWithString:filePath];

&安培;然后试试。