2014-05-22 78 views
4

我正在爲我的應用程序使用Brad Larson的偉大庫GPUImage。目前我遇到了一個問題。我的應用程序捕捉10秒的視頻,然後允許filter申請。在GPUImageMovie中應用filters時,我無法暫停播放並應用新的filter,這樣視頻就會連續播放,無需從頭開始播放。GPUImageMovie在應用篩選器時暫停

我看到一個開放的github問題here。如果有人遇到類似問題並找到解決方案,請發佈您的答案。提前致謝。

回答

8

最後,我經過太多的搜索並嘗試修復了這個問題。我們需要用AVPlayerItem而不是URL來啓動GPUImageMovie。我從here得到這個有價值的線索。

我張貼我目前在我的應用程序中使用的代碼,它按預期工作。但iPod 6.1中存在性能問題,我正在致力於增強相同的功能。

初始方法設置GPUImageMovie,

- (void)setupVideo 
{ 
    playerItem = [[AVPlayerItem alloc]initWithURL:self.recordSession.outputUrl]; 
    player = [AVPlayer playerWithPlayerItem:playerItem]; 

    movieFile = [[GPUImageMovie alloc] initWithPlayerItem:playerItem]; 

    movieFile.runBenchmark = YES; 
    movieFile.playAtActualSpeed = YES; 
    [self.view sendSubviewToBack:self.videoView]; 

    [movieFile addTarget:filter]; 
    [filter addTarget:self.videoView]; 

    [movieFile startProcessing]; 
    movieRunning = YES; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     self.playButton.hidden = YES; 
    }); 

    player.rate = 1.0; 
} 

這種方法被稱爲當在過濾器上按鈕用戶點擊。

- (void)filterClicked:(UIButton *)button 
{ 
    // Set paused time. If player reaches end of the video, set pausedTime to 0. 
    if (CMTIME_COMPARE_INLINE(pausedTime, !=, player.currentItem.asset.duration)) { 
     pausedTime = player.currentTime; 
    } else { 
     pausedTime = CMTimeMake(0, 600.0); 
    } 
    [self.videoView setBackgroundColor:[UIColor clearColor]]; 

    [movieFile cancelProcessing]; 

    switch (button.tag) 
    { 
    case 0: 
     filter = [[GPUImageFilter alloc] init]; 
     break; 
    case 1: 
     filter = [[GPUImageColorInvertFilter alloc] init]; 
     break; 
    case 2: 
     filter = [[GPUImageEmbossFilter alloc] init]; 
     break; 
    case 3: 
     filter = [[GPUImageGrayscaleFilter alloc] init]; 
     break; 
    default: 
     filter = [[GPUImageFilter alloc] init]; 
     break; 
    } 

    [self filterVideo]; 

} 

過濾器生成後,視頻播放繼續在此方法中處理。

- (void)filterVideo { 

    // AVPlayerItem is initialized with required url 

    playerItem = [[AVPlayerItem alloc]initWithURL:self.outputUrl]; 
    [player replaceCurrentItemWithPlayerItem:playerItem]; 

    //GPUImageMovie is initialized with AVPlayerItem 

    movieFile = [[GPUImageMovie alloc] initWithPlayerItem:playerItem]; 

    movieFile.runBenchmark = YES; 
    movieFile.playAtActualSpeed = YES; 

    // Adding targets for movieFile and filter 

    [movieFile addTarget:filter]; 
    [filter addTarget:self.videoView]; // self.videoView is my GPUImageView 

    [movieFile startProcessing]; 
    movieRunning = YES; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     self.playButton.hidden = YES; 
    }); 

    // Player rate is set to 0 means player is paused 

    [player setRate:0.0]; 

    // Seeking to the point where video was paused 

    if (CMTIME_COMPARE_INLINE(pausedTime, !=, player.currentItem.asset.duration)) { 
     [player seekToTime:pausedTime]; 
    } 
    [player play]; 
}