【发布时间】:2018-06-23 08:36:34
【问题描述】:
我正在尝试将样本从 CMSampleBufferRef 中取出并放入浮点数组中以更改音高。我在文档中找到了一个方法 CMSampleBufferCallForEachSample 但不知道如何使用它,也没有示例或解释。请问我这个东西绕了一段时间,有人可以帮忙吗?
【问题讨论】:
标签: objective-c
我正在尝试将样本从 CMSampleBufferRef 中取出并放入浮点数组中以更改音高。我在文档中找到了一个方法 CMSampleBufferCallForEachSample 但不知道如何使用它,也没有示例或解释。请问我这个东西绕了一段时间,有人可以帮忙吗?
【问题讨论】:
标签: objective-c
我正在解决类似的问题。我需要从一个 SampleBuffer 中拆分 AAC 样本。
...
CMSampleBufferCallForEachSample(sampleBuffer, &sampler, NULL);
...
static OSStatus sampler(CMSampleBufferRef sampleBuffer, CMItemCount index, void *refcon) {
CMTime presentationTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
CMBlockBufferRef buff = CMSampleBufferGetDataBuffer(sampleBuffer);
UInt32 timeStamp = (1000*presentationTimeStamp.value) / presentationTimeStamp.timescale;
size_t size = CMBlockBufferGetDataLength(buff);
void *sampleData = malloc(size);
CMBlockBufferCopyDataBytes(buff, 0, size, sampleData);
NSData * data = [NSData dataWithBytes:sampleData length:size];
NSLog(@"Audio %ldx samples of size %lu has been transfered at time %ld", CMSampleBufferGetNumSamples(sampleBuffer), size, timeStamp);
}
但是每个sampleBuffer中的第一个样本只调用一次回调,即使有76个样本的数组并且CMSampleBufferGetNumSamples返回这个数字
我找到了很好的解决方案 here(方法 fillMovieSampleBuffer 第 283 行)
CoreMedia 框架的文档check this URL
@function CMSampleBufferCallForEachSample
@abstract Calls a function for every individual sample in a sample buffer.
@discussion Temporary sample buffers will be created for individual samples,
referring to the sample data and containing its timing, size and attachments.
The callback function may retain these sample buffers if desired.
If the callback function returns an error, iteration will stop immediately
and the error will be returned.
If there are no sample sizes in the provided sample buffer, kCMSampleBufferError_CannotSubdivide will be returned.
This will happen, for example, if the samples in the buffer are non-contiguous (eg. non-interleaved audio, where
the channel values for a single sample are scattered through the buffer).
【讨论】: