Objective-c/IOS: Reversing the bytes in NSMutabled

2019-07-24 00:26发布

问题:

In my application I have the bytes of an audio file stored in NSMutableData like so:

NSMutableData *data = [NSMutableData dataWithBytes:byteData length:len];

I can then later play this using

player = [[AVAudioPlayer alloc] initWithData:data error:&error];

My question is what code would reverse the bytes in the data object, so that when played the sound is backwards?

Any help is very much appreciated.

By the way, the byteData of the audio is obtained using:

    NSMutableData *wave =[NSMutableData dataWithContentsOfURL:url];

NSUInteger len = [wave length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [wave bytes], len);

回答1:

DaveSmith122 is right. You cannot just reverse the byte data. I have achieved the same effect using CoreAudio and AudioUnits. Use ExtFileReader C API to read the file into lPCM buffers and then you can reverse the buffers as needed.