1

我玩了 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];
}

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

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

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

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

你们有些人有什么想法吗?

谢谢

4

1 回答 1

2

found that the -11803 was due to the fact that the captureManager session was not running (found the answer on Stackoverflow but i did not understand it very fast...)

at the end of the video preview play, in playComplete, I added a test to check the capture manager session status, to make it run if this was not the case anymore.

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

It is working now, but if anyone knows why the video preview stops the captureManager session, I would be happy to understand it.

P

于 2013-04-05T13:06:22.363 回答