2012-08-09 90 views
0

我需要某處保存,然後檢索Sint16(2字節)。保存並從NSMutableData檢索Sint16

我得到:

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]; 

不過這回不是我放在相同的。

那麼有什麼解決方案?

謝謝。

回答

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); 
0

您只複製2字節變量中的一個字節。嘗試一次追加兩個字節到NSMutableData。

[recordedData appendBytes: frames length:sizeof(SInt16)];