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

Bey*_*ity 4 audio avfoundation ios gpuimage

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

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

那么,我该如何实现呢?

Bey*_*ity 7

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

  1. GPUImageMovie.h,

    @property(readwrite, nonatomic) BOOL playSound;

  2. GPUImageMovie.m,@interface GPUImageMovie ()像这样更新.

    @interface GPUImageMovie() <AVPlayerItemOutputPullDelegate> 
    {
        // Add this
        BOOL hasAudioTrack;
    
        .........
        ........
    
       // Add all below three lines
       AVPlayer *theAudioPlayer;
       CFAbsoluteTime startActualFrameTime;
       CGFloat currentVideoTime;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 之后,在-(void)startProcessing方法中,添加以下行.

    - (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;
    
                .......
                .......
            });
       }];
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 再次,在-(AVAssetReader*)createAssetReader更新它,

    - (AVAssetReader*)createAssetReader
    {
        .......
        .......  
        BOOL shouldRecordAudioTrack = (([audioTracks count] > 0) &&  (self.audioEncodingTarget != nil));
    
        // Add this
        hasAudioTrack = ([audioTracks count] > 0);
    
        .........
    }
    
    Run Code Online (Sandbox Code Playgroud)
  5. - (void)processAsset,

    - (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];
       }
    
       .........
       .........
    }
    
    Run Code Online (Sandbox Code Playgroud)
  6. -(void)endProcessing,

    - (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];
       }
    
       .........
       .........
    }
    
    Run Code Online (Sandbox Code Playgroud)
  7. -(void)setupSound最后添加新方法,

    // Add this
    - (void)setupSound
    {
       if (theAudioPlayer != nil)
       {
           [theAudioPlayer pause];
           [theAudioPlayer seekToTime:kCMTimeZero];
           theAudioPlayer = nil;
       }
    
       theAudioPlayer = [[AVPlayer alloc] initWithURL:self.url];
    }
    
    Run Code Online (Sandbox Code Playgroud)
  8. 最后,当您创建GPUImageMovie对象时,不要忘记将playSound标志设置为YES.

    像这样 : movieFile.playSound = YES;

    希望这可以帮助.