[ios]无法播放视频记录仪由 GPUImage

标签: ios Objective-C
发布时间: 2017/3/21 22:57:04
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

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

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

解决方法 1:

我看见在一两件事情,看起来不正确。

首先关闭你写直接向 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 建议吗?MoviePlayer 呆在那里不停地缓冲的事实是最有可能引起的因为它不会被保留。

将属性添加到您的 viewController 接口︰

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;

并实例化你 MPMoviewPlayerController :

self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
赞助商