首页 文章

播放使用AVFoundation录制的视频

提问于
浏览
0

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

- (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 回答

  • 2

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

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

    并试图从捆绑路径播放 . 使用相同的路径播放录音 .

    • 首先,检查您的视频是否被正确记录和保存 . 从您的代码中,视频被保存为临时目录 . 在路径上检查视频 . 如果它存在与否 .
    NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];
    NSLog(@"%@", 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]; 
    }
    
  • 0

    替换你的这一行:

    NSURL *url = [NSURL fileURLWithPath:filePath];

    有了这个:

    NSURL *url=[NSURL URLWithString:filePath];
    

    然后试试 .

相关问题