更新后 AudioBufferList 字节不匹配

After updating AudioBufferList Bytes mismatch

************************ 总计:1250 字节 ********* **************

Printing description of viewController->dataAudioBytes: f3fed5fe 83fe66fd b4fc78fc e1fb5afb 2cfb4dfb b3fb8ffb 32fad5f9 64fbacfc 7dfdc3fd 4bfe8cff 1500f2ff edffb400 3c01ef01 56038d04 a704bd04 5e054205 c8053206 5505e804 74048a04 9a042504 2f045f03 04027400 e7fe63fe effda4fd 1ffd5dfc 81fc64fd 9dfd9efd 81fe51ff 16ffadfe a1fe4bff 57ffdefe 1aff3a00 2301b500 d1ffacff dbfff1ff 5600c500 97000200 b9ff3700 8700e400 7501d801 4302e001 4e015b01 a9015901 28019101 7d015201 5501c101 5e021f03 9603bb02 67010a01 2c012301 5400dbfe 65fed1fe c0ff2b00 d8ffdbff 20002300 47007e00 63006400 5c00e800 9d016f01 76008aff 2bff82ff 4dff4bfe f6fc3afc 7efc52fd 1efee3fe d2fe69fe 8dfe02fe 8dfd0efe effca7fc b7fd87fd d8fcd4fc 6dfd6efe fdfeb8ff 1e0003ff a0fe07ff 5bffb6ff 7eff43ff bdfe56ff e6ff0f00 47009300...

****************** 从 1250 字节 中选择 320 字节 *** **************

Printing description of bytes320: f3fed5fe 83fe66fd b4fc78fc e1fb5afb 2cfb4dfb b3fb8ffb 32fad5f9 64fbacfc 7dfdc3fd 4bfe8cff 1500f2ff edffb400 3c01ef01 56038d04 a704bd04 5e054205 c8053206 5505e804 74048a04 9a042504 2f045f03 04027400 e7fe63fe effda4fd 1ffd5dfc 81fc64fd 9dfd9efd 81fe51ff 16ffadfe a1fe4bff 57ffdefe 1aff3a00 2301b500 d1ffacff dbfff1ff 5600c500 97000200 b9ff3700 8700e400 7501d801 4302e001 4e015b01 a9015901 28019101 7d015201 5501c101 5e021f03 9603bb02 67010a01 2c012301 5400dbfe 65fed1fe c0ff2b00 d8ffdbff 20002300 47007e00 63006400 5c00e800 9d016f01 76008aff 2bff82ff 4dff4bfe f6fc3afc 7efc52fd 1efee3fe d2fe69fe 8dfe02fe 8dfd0efe effca7fc b7fd87fd d8fcd4fc 6dfd6efe fdfeb8ff 1e0003ff a0fe07ff 5bffb6ff 7eff43ff bdfe56ff e6ff0f00 47009300

- (void)viewDidLoad
{
    [super viewDidLoad];

       NSData *bytes320 = [[NSData alloc]init];
       bytes320 = [viewController.dataAudioBytes subdataWithRange:NSMakeRange(0, 320)];

       ====================== Update AudioBufferList =================

       AudioBufferList *ioData = (AudioBufferList *)malloc(offsetof(AudioBufferList, mBuffers) + sizeof(AudioBuffer *));
       ioData->mNumberBuffers = 1;
       ioData->mBuffers[0].mNumberChannels = (UInt32)1;
       ioData->mBuffers[0].mDataByteSize = (UInt32)[bytes320 length];
       ioData->mBuffers[0].mData = (__bridge void*)bytes320;
}

- (void)encodeAudio:(AudioBufferList *)data timestamp:(const AudioTimeStamp *)timestamp
  {
              NSData *dataBytes = [NSData dataWithBytes:data->mBuffers[0].mData length:data->mBuffers[0].mDataByteSize];
  }

****************** 更改 AudioBufferList 字节 ******************

 NSData *dataBytes = [NSData dataWithBytes:data->mBuffers[0].mData
 length:data->mBuffers[0].mDataByteSize];

Printing description of dataBytes: 29a067a8 a1210000 40010000 00000000 f3fed5fe 83fe66fd b4fc78fc e1fb5afb 2cfb4dfb b3fb8ffb 32fad5f9 64fbacfc 7dfdc3fd 4bfe8cff 1500f2ff edffb400 3c01ef01 56038d04 a704bd04 5e054205 c8053206 5505e804 74048a04 9a042504 2f045f03 04027400 e7fe63fe effda4fd 1ffd5dfc 81fc64fd 9dfd9efd 81fe51ff 16ffadfe a1fe4bff 57ffdefe 1aff3a00 2301b500 d1ffacff dbfff1ff 5600c500 97000200 b9ff3700 8700e400 7501d801 4302e001 4e015b01 a9015901 28019101 7d015201 5501c101 5e021f03 9603bb02 67010a01 2c012301 5400dbfe 65fed1fe c0ff2b00 d8ffdbff 20002300 47007e00 63006400 5c00e800 9d016f01 76008aff 2bff82ff 4dff4bfe f6fc3afc 7efc52fd 1efee3fe d2fe69fe 8dfe02fe 8dfd0efe effca7fc b7fd87fd d8fcd4fc 6dfd6efe fdfeb8ff 1e0003ff a0fe07ff 5bffb6ff

您应该将 mData 指向 bytes320 的字节而不是对象本身。

ioData->mBuffers[0].mData = bytes320.bytes; 

自己寻找解决方案。

NSData *data = [NSData dataWithContentsOfFile:filePath];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);

Here