Ios 播放用AVFoundation录制的视频

Ios 播放用AVFoundation录制的视频,ios,objective-c,avfoundation,avplayer,Ios,Objective C,Avfoundation,Avplayer,我想用同样的按钮开始和停止录音。我想用另一个按钮来播放录音。以下是我所拥有的: - (IBAction)recordVideo:(id)sender { if(!self.movieOutput.isRecording) { NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"]; NSFileManager *manager = [[NS

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

- (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。

替换此行:

NSURL*url=[NSURL fileURLWithPath:filePath]

为此:

NSURL *url=[NSURL URLWithString:filePath];

&然后再试。

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

NSString *outputPath = [NSTemporaryDirectory()stringByAppendingPathComponent:@"output.mp4"];
并尝试从捆绑路径播放。也使用相同的路径播放录制

  • 首先,检查您的视频是否被正确录制和保存。从您的代码中,视频保存在临时目录中。检查路径处的视频。如果它存在或不存在

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

    NSString *outputPath = [NSTemporaryDirectory()stringByAppendingPathComponent:@"output.mp4"];
    _outputPath = outputPath;
    
  • 要播放视频,请尝试以下操作:

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

  • 此值不正确<代码>\u outputPath
    您能显示它的定义和分配位置吗?像这样?NSURL*fileURL=[NSURL fileURLWithPath:_outputPath];self.avPlayer=[avPlayer playerWithURL:fileURL];