0

我需要在某个地方保存然后检索 Sint16(2 个字节)。

我得到:

SInt16* frames = (SInt16*)inBuffer->mAudioData;

并想保存&frames[i]在以后可以轻松检索的某个地方(NSMutableData?)。我试图像这样保存(循环中):

[recordedData appendBytes:&frames[i] length:1];

并检索:

    SInt16* framesRetrieve ;

    //sets up mybytes buffer and reads in data from myData
    framesRetrieve = (SInt16*)malloc([mutableData framesRetrieve]);

    [mutableData getBytes:framesRetrieve];

但是这个回报和我投入的不一样。

那么有什么解决办法呢?

谢谢。

4

2 回答 2

1

您需要知道数据的长度,然后您可以将整个缓冲区存储在一个NSMutableData对象中:

储藏:

SInt16 *frames = ...;
NSUInteger length = ...;    // Assuming number of elements in frames, not bytes!   
NSMutableData *data = [[NSMutableData alloc] initWithCapacity:0];
[data appendBytes:(const void *)frames length:length * sizeof(SInt16)];

要检索:

SInt16 *frames = (SInt16 *)[data bytes];
NSUInteger length = [data length] / sizeof(SInt16);
于 2012-08-09T06:53:10.860 回答
0

您只是从 2 字节变量中复制一个字节。尝试一次将两个字节附加到 NSMutableData。

[recordedData appendBytes: frames length:sizeof(SInt16)];
于 2012-08-09T06:41:28.407 回答