6

我正在使用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];

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

4

2 回答 2

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];
    }
于 2013-04-22T11:35:22.873 回答
0

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

首先,您将直接写入NSHomeDirectory返回的应用程序目录,但该目录是不可写的(请参阅参考资料)。请改为写入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 的建议制作属性/ivar?moviePlayer 无休止地留在那里缓冲的事实很可能是因为它没有被保留。

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

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;

并实例化你的MPMoviewPlayerController

self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
于 2013-04-22T12:46:19.543 回答