【问题标题】:Playing a video recorded with AVFoundation播放用 AVFoundation 录制的视频
【发布时间】:2015-12-08 17:24:16
【问题描述】:

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

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

当我运行并按下播放按钮时,我没有收到任何错误,也没有看到任何 avplayer。

【问题讨论】:

  • 这个值不正确。 _outputPath你能说明它是在哪里定义和分配的吗?

标签: ios objective-c avfoundation avplayer


【解决方案1】:

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

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

【讨论】:

  • 喜欢这个? NSURL *fileURL = [NSURL fileURLWithPath:_outputPath]; self.avPlayer = [AVPlayer playerWithURL:fileURL];
【解决方案2】:

替换你的这一行:

NSURL *url = [NSURL fileURLWithPath:filePath];

用这个:

NSURL *url=[NSURL URLWithString:filePath];

,然后尝试。

【讨论】:

    猜你喜欢
    • 2011-10-22
    • 1970-01-01
    • 2015-04-23
    • 2016-05-15
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多