Reputation: 1460
I am making and app to transfer audio to the receiving server. I want to encode the audio buffer in order to stream. I made a class which stores this buffer and some other information. When I decode the data after encoding the main object, these two objects have different audio buffer.
So the conclusion is I am making a mistake in encoding/decoding. Here is my code for encoding the buffer
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeBytes:&_data length:sizeof(float *)];
[aCoder encodeInt:_numFrames forKey:numFrames];
[aCoder encodeInt:_numChannels forKey:numChannels];
}
NOTE _data
has (float *)
c data type
The decoding logic goes here
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
NSUInteger length = 0;
{
const uint8_t *buffer = [aDecoder decodeBytesForKey:data returnedLength:&length];
assert(length);
memcpy(&_data, buffer, length);
}
_numFrames = [aDecoder decodeInt32ForKey:numFrames];
_numChannels = [aDecoder decodeInt32ForKey:numChannels];
}
return self;
}
I am using this method to compare if they are identical
- (BOOL)isEqual:(id)object {
AudioPacket *compareObject = (AudioPacket *)object;
return (_data == compareObject.data && _numFrames == compareObject.numFrames && _numChannels == compareObject.numChannels);
}
Upvotes: 0
Views: 358
Reputation: 7044
Not sure, but when you encode _data
you don't give it a key:
[aCoder encodeBytes:&_data length:sizeof(float *)];
When you decode _data
you use a key data
:
const uint8_t *buffer = [aDecoder decodeBytesForKey:data returnedLength:&length];
Perhaps you want a version of encodeBytes
that also takes a key
encodeBytes:length:forKey:
Upvotes: 1