gpt4 book ai didi

objective-c - 代理 AVCaptureVideoDataOutput 和 AVCaptureAudioDataOutput

转载 作者:可可西里 更新时间:2023-11-01 06:10:53 24 4
gpt4 key购买 nike

我尝试从 AVCaptureVideoDataOutputAVCaptureAudioDataOutput 获取 CMSampleBufferRef

AVCamRecorder.h

#import <AVFoundation/AVFoundation.h>

@interface AVCamRecorder : NSObject {
}
@property (nonatomic,retain) AVCaptureVideoDataOutput *videoDataOutput;
@property (nonatomic,retain) AVCaptureAudioDataOutput *audioDataOutput;

@end

AVCamRecorder.m

#import "AVCamRecorder.h"
#import <AVFoundation/AVFoundation.h>

@interface AVCamRecorder (VideoDataOutputDelegate) <AVCaptureVideoDataOutputSampleBufferDelegate>
@end
@interface AVCamRecorder (AudioDataOutputDelegate) <AVCaptureAudioDataOutputSampleBufferDelegate>
@end


-(id)initWithSession:(AVCaptureSession *)aSession
{

self = [super init];
if (self != nil) {

//AudioDataoutput
AVCaptureAudioDataOutput *aAudioDataOutput = [[AVCaptureAudioDataOutput alloc] init];

//VideoDataoutput
AVCaptureVideoDataOutput *aMovieDataOutput = [[AVCaptureVideoDataOutput alloc] init];


if ([aSession canAddOutput:aAudioDataOutput]) {
[aSession addOutput:aAudioDataOutput];
}
if ([aSession canAddOutput:aMovieDataOutput]) {
[aSession addOutput:aMovieDataOutput];
}

[aAudioDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
[aMovieDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

[self setAudioDataOutput:aAudioDataOutput];
[self setVideoDataOutput:aMovieDataOutput];

[self setSession:aSession];

}
return self;
}

@implementation AVCamRecorder (VideoDataOutputDelegate)
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
NSLog(@"VideoDataOutputDelegate = %@", captureOutput);
}
@end

@implementation AVCamRecorder (AudioDataOutputDelegate)
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
NSLog(@"AudioDataOutputDelegate = %@", captureOutput);
}
@end

奇怪的是,我在“@implementation AVCamRecorder (AudioDataOutputDelegate)”中得到了视频数据

AudioDataOutputDelegate = <AVCaptureVideoDataOutput: 0x208a7df0>

我调换了“@implementation AVCamRecorder (VideoDataOutputDelegate)”和“@implementation AVCamRecorder (VideoDataOutputDelegate)”的顺序,我得到了

VideoDataOutputDelegate = <AVCaptureVideoDataOutput: 0x208a7df0>

我似乎无法设置 2“captureOutput:didOutputSampleBuffer:fromConnection:”。否则,数据将进入任何一个。

或者,我是否错误地设置了“@implementation AVCamRecorder (VideoDataOutputDelegate)”和“@implementation AVCamRecorder (AudioDataOutputDelegate)”?

我想我不需要单独的回调,但我只是想知道哪里出了问题。

提前感谢您的帮助。

最佳答案

你在同一个类上定义了 2 个类别

AVCamRecorder (VideoDataOutputDelegate)
AVCamRecorder (AudioDataOutputDelegate)

声明相同的方法

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection;

这会导致未定义的行为。参见 Avoid Category Method Name Clashes在“使用 Objective-C 编程”指南中:

If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime.
...

因此您的设置无法正常工作。你可以改为

  • 定义两个单独的,一个作为音频代理,一个作为视频代理,
  • 定义一个类类别作为音频+视频委托(delegate)(并检查调用它的函数的回调方法),
  • 只需使用 AVCamRecorder 本身作为音频 + 视频代理。

关于objective-c - 代理 AVCaptureVideoDataOutput 和 AVCaptureAudioDataOutput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14102548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com