无法通过GPUImage播放录像机

时间:2013-04-14 17:26:35

标签: ios objective-c gpuimage m4v

我正在使用GPUImage库将视频录制到文件系统上的文件中。我把它保存为m4v文件。这是我正在使用的代码:

    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:recordingDestination];
    unlink([pathToMovie UTF8String]);
    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

    movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(1280.0, 720.0)];
    [videoCameraFilter addTarget:movieWriter];

    movieWriter.shouldPassthroughAudio = YES;
    movieFile.audioEncodingTarget = movieWriter;
    [movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];

    [movieWriter startRecording];
    [movieFile startProcessing];

    [movieWriter setCompletionBlock:^{
        [videoCameraFilter removeTarget:movieWriter];
        [movieWriter finishRecording];
    }];

这会录制m4v视频。然后我尝试用MPMoviePlayerController

播放它
    NSString *videoPath = [NSString stringWithFormat:@"%@/%@", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"], [libraryFiles objectAtIndex:selectedCell]];
    NSLog(videoPath);
    NSURL *url=[[NSURL alloc] initWithString:videoPath];
    MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDonePressed) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];

    moviePlayer.controlStyle=MPMovieControlStyleDefault;
    [moviePlayer play];
    [self.view addSubview:moviePlayer.view];
    [moviePlayer setFullscreen:YES animated:YES];

然而,玩家只是开始缓冲而没有任何反应。视频无法播放。玩家只是永远缓冲。应用程序没有崩溃,控制台中没有警告,所以我不知道是什么问题。

2 个答案:

答案 0 :(得分:0)

试试这个:

首先检查videoPath它是否显示正确的路径?

.h文件

   MPMoviePlayerController*     player

的.m        @synthesize播放器;

    -(void) viewWillAppear:(BOOL)animated {
            [super viewWillAppear:animated];

           player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:videoPath]];
           player.scalingMode = MPMovieScalingModeFill;
           player.movieSourceType = MPMovieSourceTypeFile;
           player.view.frame = CGRectMake(0, 45, 320, 400);
           player.shouldAutoplay = YES;
           [player prepareToPlay];
           [self.view addSubview:player.view];
           [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];

           [player play];
    }

    #pragma mark MoviPlayer Method

    - (void) movieFinishedCallback:(NSNotification*) aNotification {
            MPMoviePlayerController *player1 = [aNotification object];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player1];
            [player stop];
            [player1.view removeFromSuperview];

            player1 = nil;
            [self.navigationController popViewControllerAnimated:YES];
    }

    -(void) dealloc {
            [player release];
            [super dealloc];
    }

答案 1 :(得分:0)

我看到一些看起来不正确的事情。

首先,你直接写入NSHomeDirectory,它返回的应用程序目录不可写(参见reference)。 请改为写入Documents文件夹或Library/Caches文件夹。

其次,我不知道如何定义videoCameraFilter。您需要确保将其添加为movieFile

的目标
GPUImageMovie *movieFile = [[GPUImageMovie alloc] initWithURL:sampleURL];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathToMovie = [documentsDirectory stringByAppendingPathComponent:@"filtered-movie.m4v"];
unlink([pathToMovie UTF8String]);
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

GPUImageMovieWriter *movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(1920.0, 1080.0)];
GPUImageBoxBlurFilter * videoCameraFilter = [[GPUImageBoxBlurFilter alloc] init]; // just a random filter...
videoCameraFilter.blurSize = 2.0;
[movieFile addTarget:videoCameraFilter];
[videoCameraFilter addTarget:movieWriter];

movieWriter.shouldPassthroughAudio = YES;
movieFile.audioEncodingTarget = movieWriter;
[movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];

[movieWriter startRecording];
[movieFile startProcessing];

__weak GPUImageMovieWriter *weakMoviewWriter = movieWriter;
[movieWriter setCompletionBlock:^{
    [movieFile removeTarget:weakMoviewWriter];
    [weakMoviewWriter finishRecording];
}];

*编辑:

你有没有尝试过@ moviePlayer @SAMIR RATHOD建议的@property (nonatomic, strong) MPMoviePlayerController *moviePlayer; 属性/ ivar?电影播放器​​无休止地停留在那里的事实很可能是因为它没有被保留所致。

在viewController的界面中添加一个属性:

MPMoviewPlayerController

并实例化您的self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

{{1}}