作者ryanliang (暑假闲闲的大学生~~)
看板MacDev
标题[问题] Buffer没有办法储存Audio data
时间Wed Oct 13 19:53:42 2010
各位板上先进
我想要在iSight streaming的时候 同时截取frames及audio data
目前遇到的情况是
当我已经可以动态撷取到audio data的时候(可以读到在串流时的所有资料)
我想利用一个buttom 撷取一组audio data到某一个buffer中
不过现在的问题就是
在动态撷取时mCurrentBuffer都会有值(length,duration...等等)
但是在按下capture键後
再取出来的值就都是0了 我很纳闷为什麽不能保留
以下是动态撷取vidoe跟audio的程式码:
// Video部分 QTCaptureDecompressedVideoOutput 的delegate method
- (void)captureOutput:(QTCaptureOutput *)captureOutputdidOutputVideo
Frame:(CVImageBufferRef)videoFramewithSampleBuffer:(QTSampleBuffer *)
sampleBufferfromConnection:(QTCaptureConnection *)connection
{
CVImageBufferRef imageBufferToRelease;
CVBufferRetain(videoFrame);
@synchronized (self)
{
imageBufferToRelease = mCurrentImageBuffer;
mCurrentImageBuffer = videoFrame; //把目前的Frame放到buffer中
}
if (mCurrentImageBuffer)
{
NSLog(@"Get Image Data !");
NSLog(@"%p",mCurrentImageBuffer);
}
CVBufferRelease(imageBufferToRelease);
}
// Audio部分 QTCaptureDecompressedAudioOutput 的delegate method
-(void)captureOutput:(QTCaptureOutput *)captureOutputdidOutputAudio
SampleBuffer:(QTSampleBuffer *)sampleBufferfromConnection:(QTCapture
Connection *)connection
{
QTSampleBuffer *audioBufferRelese = nil;
[sampleBuffer retain];
@synchronized (self)
{
audioBufferRelese = mCurrentAudioBuffer;
mCurrentAudioBuffer = sampleBuffer; //把目前的audio data放到buffer
}
if (mCurrentAudioBuffer)
{
NSLog(@"Get Audio Data !");
NSLog(@"%i",[mCurrentAudioBuffer duration]); //这边可以抓到值
}
[audioBufferRelese release];
}
接下来是撷取单一画面及单一audio data的程式码:
// 按下addFrame後 可以存入一张点阵图
- (IBAction)addFrame:(id)sender
{
CVImageBufferRef imageBuffer;
@synchronized (self) //把frame放进buffer
{
imageBuffer = CVBufferRetain(mCurrentImageBuffer);
}
if (imageBuffer)
{
// Create an NSImage and add it to the movie
NSCIImageRep *imageRep = [NSCIImageRep imageRepWithCIImage:[CIImage
imageWithCVImageBuffer:imageBuffer]];
NSImage *image = [[[NSImage alloc] initWithSize:[imageRep size]]
autorelease];
[image addRepresentation:imageRep]; //加入imageRep的内容到image中
CVBufferRelease(imageBuffer); //释放buffer
//将image转成bitmap档格式(因为bitmap是未经压缩过的图片档,只是多了档头
NSBitmapImageRep *bitMapImageRep = [[NSBitmapImageRep alloc]
initWithData:[image TIFFRepresentation]];
[image addRepresentation:bitMapImageRep]; //把TIFF加到image中
NSData *mData = [bitMapImageRep representationUsingType:NSBMPFile
Typeproperties:nil];
[mData writeToFile:@"/Users/Shared/test.bmp" atomically:NO];
}
**********最重要的就是以下这一段 因为这一段不能work********
// 按下capture时 要撷取一份audio data
- (IBAction)capture:(id)sender
{
QTSampleBuffer *audioBuffer = nil;
@synchronized (self)
{
audioBuffer = [mCurrentAudioBuffer retain];
}
if (audioBuffer) //有收到audio data就会进入
{
NSLog(@"Capture Audio Raw Data !");
NSLog(@"%i",[audioBuffer duration]);
}
[audioBuffer release];
}
基本上video跟audio的方发都差不多 只差在用的brffer不同
在addFrame时CVImageBuffer能够明确将资料留住
到了capture时 QTSampleBuffer就没有办法再event发生时留住里面的资料
所以当动态撷取audio在跑的时候 按下capture时
就不会的得到正确的512 而是得到0
不知道是不是buffer的方式用错
我这几天找了很多的buffer都没办法合用
不知道是甚麽原因造成这样的结果
希望版上的先进能够帮帮我
感谢!!
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.219.177.46