convert AVAudioRecorder to NSString or bytes

1.2k Views Asked by At

I am trying to send AVAudioRecorder in Json , i would like to convert it to NSString or Byte.. any idea?

2

There are 2 best solutions below

0
On BEST ANSWER

Never tried but maybe this ?

// Get the recording
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:filePath settings:recordSetting error:&error];
....
// Convert to NSData
NSData *theData = [[NSData alloc] initWithContentsOfFile:filePath];
// Convert to NSString
NSString* dataAsString = [NSString stringWithUTF8String:[theData bytes]];
0
On

Once you have completed a recording with AVAudioRecorder, you can retrieve the recorded file using your AVAudioRecorder's url property. Then you can initialize an NSData with the url:

NSData *data = [NSData dataWithContentsOfURL:recorder.url];

To send it as JSON, you need to base64 the data. I recommend this project which has a category for doing this: https://github.com/l4u/NSData-Base64

You'll need to #import "NSData+Base64.h" into your source and then you can get the base64 string from your NSData by calling base64EncodedString on it.