I want to reverse bytes of NSMutableData
.
我想要反转NSMutableData的字节。
I am generating nsmutabledata object from a audio file. and playing it using data. my purpose is play audio reverse.
我正在从音频文件生成nsmutabledata对象。并使用数据播放它。我的目的是播放音频反转。
If I can reverse the NSMutableData
I will be success.
如果我可以反转NSMutableData,我将获得成功。
Here is my code
这是我的代码
NSString *inputVideoPath = [[NSBundle mainBundle] pathForResource:@"chalne" ofType:@"mp3"];
NSMutableData *wave=[NSMutableData dataWithContentsOfURL:[NSURL fileURLWithPath:inputVideoPath]];
avPlayer = [[AVAudioPlayer alloc] initWithData:wave error:nil ];
[avPlayer prepareToPlay];
[avPlayer play];
3 个解决方案
#1
2
Simply reversing the order of the bits will not work, because a bytestream of sound data has a specific format. You'll want to reverse the samples, not the bytestream as a whole. However, as to answer 'how to reverse the bytes of an NSData', this ought to work (typed it out, may have typos):
简单地反转位的顺序将不起作用,因为声音数据的字节流具有特定格式。您需要反转样本,而不是整个字节流。但是,至于回答'如何反转NSData的字节',这应该工作(键入它,可能有拼写错误):
NSData *myData;
const char *bytes = [myData bytes];
char *reverseBytes = malloc(sizeof(char) * [myData length]);
int index = [myData length] - 1;
for (int i = 0; i < [myData length]; i++)
reverseBytes[index--] = bytes[i];
NSData *reversedData = [NSData dataWithBytes:reverseBytes length:[myData length]]
free(reverseBytes);
#2
1
I fixed the above example and tested it. now it can be used in a category:
我修复了上面的例子并进行了测试。现在它可以用在一个类别中:
- (NSData*) reversedData
{
NSData *myData = self;
const char *bytes = [myData bytes];
NSUInteger datalength = [myData length];
char *reverseBytes = malloc(sizeof(char) * datalength);
NSUInteger index = datalength - 1;
for (int i = 0; i < datalength; i++)
reverseBytes[index--] = bytes[i];
NSData *reversedData = [NSData dataWithBytesNoCopy:reverseBytes length: datalength freeWhenDone:YES];
return reversedData;
}
#3
0
-(NSData *)reverseData
{
NSMutableData *data = [[NSMutableData alloc] init];
for(int i = (int)self.length - 1; i >=0; i--){
[data appendBytes: &self.bytes[i] length:1];
}
return [data copy];
}
#1
2
Simply reversing the order of the bits will not work, because a bytestream of sound data has a specific format. You'll want to reverse the samples, not the bytestream as a whole. However, as to answer 'how to reverse the bytes of an NSData', this ought to work (typed it out, may have typos):
简单地反转位的顺序将不起作用,因为声音数据的字节流具有特定格式。您需要反转样本,而不是整个字节流。但是,至于回答'如何反转NSData的字节',这应该工作(键入它,可能有拼写错误):
NSData *myData;
const char *bytes = [myData bytes];
char *reverseBytes = malloc(sizeof(char) * [myData length]);
int index = [myData length] - 1;
for (int i = 0; i < [myData length]; i++)
reverseBytes[index--] = bytes[i];
NSData *reversedData = [NSData dataWithBytes:reverseBytes length:[myData length]]
free(reverseBytes);
#2
1
I fixed the above example and tested it. now it can be used in a category:
我修复了上面的例子并进行了测试。现在它可以用在一个类别中:
- (NSData*) reversedData
{
NSData *myData = self;
const char *bytes = [myData bytes];
NSUInteger datalength = [myData length];
char *reverseBytes = malloc(sizeof(char) * datalength);
NSUInteger index = datalength - 1;
for (int i = 0; i < datalength; i++)
reverseBytes[index--] = bytes[i];
NSData *reversedData = [NSData dataWithBytesNoCopy:reverseBytes length: datalength freeWhenDone:YES];
return reversedData;
}
#3
0
-(NSData *)reverseData
{
NSMutableData *data = [[NSMutableData alloc] init];
for(int i = (int)self.length - 1; i >=0; i--){
[data appendBytes: &self.bytes[i] length:1];
}
return [data copy];
}