在GPUImage中应用视频过滤器时播放音频

时间:2015-08-12 09:58:46

标签: ios audio avfoundation gpuimage

我搜索了这个问题,但没有找到任何解决方案。

所以,我的问题是, 我正在使用GPUImage将视频过滤器应用于视频文件。同时,该视频的声音没有播放。我知道在应用过滤器时GPUImage不支持声音播放。

那么,我该怎么做呢?

2 个答案:

答案 0 :(得分:7)

您可以通过添加GPUImageMovie.h这样的支持来实现声音播放。

  1. @property(readwrite, nonatomic) BOOL playSound;

    GPUImageMovie.m

  2. @interface GPUImageMovie ()中,像这样更新@interface GPUImageMovie() <AVPlayerItemOutputPullDelegate> { // Add this BOOL hasAudioTrack; ......... ........ // Add all below three lines AVPlayer *theAudioPlayer; CFAbsoluteTime startActualFrameTime; CGFloat currentVideoTime; }

    -(void)startProcessing
  3. 之后,在- (void)startProcessing { // Add this currentVideoTime = 0.0f; ..... ..... // Add this if (self.playSound) { [self setupSound]; } GPUImageMovie __block *blockSelf = self; [inputAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler: ^{ runSynchronouslyOnVideoProcessingQueue(^{ ......... ......... // Add this startActualFrameTime = CFAbsoluteTimeGetCurrent() - currentVideoTime; ....... ....... }); }]; } 方法中添加以下行。

    -(AVAssetReader*)createAssetReader
  4. 再次,在- (AVAssetReader*)createAssetReader { ....... ....... BOOL shouldRecordAudioTrack = (([audioTracks count] > 0) && (self.audioEncodingTarget != nil)); // Add this hasAudioTrack = ([audioTracks count] > 0); ......... } 更新它,

    -(void)processAsset
  5. -(void)processAsset { ....... ....... if ([reader startReading] == NO) { NSLog(@"Error reading from file at URL: %@", self.url); return; } // Add this if (self.playSound && hasAudioTrack) { [theAudioPlayer seekToTime:kCMTimeZero]; [theAudioPlayer play]; } ......... ......... }

    -(void)endProcessing
  6. -(void) endProcessing { ......... ......... if (self.playerItem && (displayLink != nil)) { [displayLink invalidate]; // remove from all run loops displayLink = nil; } // Add this if (theAudioPlayer != nil) { [theAudioPlayer pause]; [theAudioPlayer seekToTime:kCMTimeZero]; } ......... ......... }

    -(void) setupSound
  7. 最后添加新方法// Add this - (void) setupSound { if (theAudioPlayer != nil) { [theAudioPlayer pause]; [theAudioPlayer seekToTime:kCMTimeZero]; theAudioPlayer = nil; } theAudioPlayer = [[AVPlayer alloc] initWithURL:self.url]; }

    GPUImageMovie
  8. 最后,当您创建playSound对象时,不要忘记将YES.标记设置为movieFile.playSound = YES;

    像这样:SELECT * FROM Table1 WHERE [name] IN (SELECT name, Count(name) FROM Table1 GROUP BY name HAVING COUNT(name)>1)

    希望这会有所帮助。

答案 1 :(得分:0)

如何在 GPUImage

应用的过滤器中暂停音频

就像我暂停电影写作时我们可以暂停movieWriter.paused = YES;的视频写作一样,我也需要暂停音频。