在objective-c中录制视频时出错

时间:2013-04-05 12:41:55

标签: objective-c video

我玩AVCam演示示例以添加允许预览录制视频的播放器(MPMoviePlayerController)

我在xib中添加了一个播放按钮以及相应的IBOutlet和IBActions +播放器:

@property (nonatomic,retain) IBOutlet UIBarButtonItem *playerButton;
@property (nonatomic, assign) MPMoviePlayerController *player;

- (IBAction)playVideo:(id)sender;

我在viewdidload中初始化播放器:

player = [[MPMoviePlayerController alloc] init];

播放动作如下

- (IBAction)playVideo:(id)sender {

     if(player) {
     [player stop];
     }
     // Create a new movie player object.
     [player setContentURL:[[[self captureManager] recorder] outputFileURL]];
     player.movieSourceType = MPMovieSourceTypeFile;
    [player prepareToPlay];
    CGRect viewInsetRect = CGRectMake( 0, 0, 200, 300);
    // Inset the movie frame in the parent view frame.
    [[player view] setFrame:viewInsetRect];

    player.scalingMode = MPMovieScalingModeAspectFill;
    player.controlStyle = MPMovieControlStyleNone;
    //player.useApplicationAudioSession = NO;

    //[self.view addSubview: [_player view]];
    [self.view addSubview:player.view];

    if(player) {
    [player play];
    }

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playComplete) name:MPMoviePlayerPlaybackDidFinishNotification object:player];

}

播放完成后调用playComplete:

-(void)playComplete {
    [player.view removeFromSuperview];
}

结果: 当我启动应用程序时,我可以录制尽可能多的视频。 当我播放最后录制的视频时,我可以毫无问题地播放它 但是在播放视频后,如果我再次尝试录制,我会在开始录制时遇到此错误:

错误Domain = AVFoundationErrorDomain Code = -11803“无法记录”UserInfo = 0x152e60 {NSLocalizedRecoverySuggestion =再次尝试录制。,AVErrorRecordingSuccessfullyFinishedKey = false,NSLocalizedDescription =无法录制}

我认为这是临时输出文件的问题,但似乎没有。

我想知道引入MPMoviePlayerController是否会以某种我无法弄清楚的方式破坏应用程序的行为。

你们有些人有任何想法吗?

由于

P

1 个答案:

答案 0 :(得分:1)

发现-11803是由于captureManager会话没有运行的事实(在Stackoverflow上找到答案,但我不太了解它......)

在视频预览播放结束时,在playComplete中,我添加了一个测试来检查捕获管理器会话状态,以便在不再出现这种情况时运行它。

-(void)playComplete {
    [player.view removeFromSuperview];
    if(![[[self captureManager] session] isRunning]) {
        // Start the session. This is done asychronously since -startRunning doesn't return until the session is running.
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [[[self captureManager] session] startRunning];
        });
    }
}

它现在正在运行,但如果有人知道为什么视频预览会停止captureManager会话,我会很乐意理解它。

P