Record and send/stream sound from iOS device to a server continuously Record and send/stream sound from iOS device to a server continuously ios ios

Record and send/stream sound from iOS device to a server continuously


look at this
in this tutorial, the sound recorded will be saved at soundFileURL, then you will just have to create an nsdata with that content, and then send it to your server.
hope this helped.

EDIT :
I just created a version that contain 3 buttons, REC, SEND and Stop :
REC : will start recording into a file.
SEND : will save what was recorded on that file in a NSData, and send it to a server, then will restart recording.
and STOP : will stop recording.
here is the code : in your .h file :

#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>@interface ViewController : UIViewController <AVAudioRecorderDelegate>@property (nonatomic, retain) AVAudioRecorder *audioRecorder;@property (nonatomic, retain) IBOutlet UIButton *recordButton;@property (nonatomic, retain) IBOutlet UIButton *stopButton;@property (nonatomic, retain) IBOutlet UIButton *sendButton;@property BOOL stoped;- (IBAction)startRec:(id)sender;- (IBAction)sendToServer:(id)sender;- (IBAction)stop:(id)sender;@end

and in the .m file :

#import "ViewController.h"@implementation ViewController@synthesize audioRecorder;@synthesize recordButton,sendButton,stopButton;@synthesize stoped;- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Release any cached data, images, etc that aren't in use.}#pragma mark - View lifecycle- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.sendButton.enabled = NO;stopButton.enabled = NO;stoped = YES;NSArray *dirPaths;NSString *docsDir;dirPaths = NSSearchPathForDirectoriesInDomains(                                               NSDocumentDirectory, NSUserDomainMask, YES);docsDir = [dirPaths objectAtIndex:0];NSString *soundFilePath = [docsDir                           stringByAppendingPathComponent:@"tempsound.caf"];NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];NSDictionary *recordSettings = [NSDictionary                                 dictionaryWithObjectsAndKeys:                                [NSNumber numberWithInt:AVAudioQualityMin],                                AVEncoderAudioQualityKey,                                [NSNumber numberWithInt:16],                                 AVEncoderBitRateKey,                                [NSNumber numberWithInt: 2],                                 AVNumberOfChannelsKey,                                [NSNumber numberWithFloat:44100.0],                                 AVSampleRateKey,                                nil];NSError *error = nil;audioRecorder = [[AVAudioRecorder alloc]                 initWithURL:soundFileURL                 settings:recordSettings                 error:&error];audioRecorder.delegate = self;if (error){    NSLog(@"error: %@", [error localizedDescription]);} else {    [audioRecorder prepareToRecord];}}- (void)viewDidUnload{    [super viewDidUnload];    // Release any retained subviews of the main view.    // e.g. self.myOutlet = nil;}- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];}- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];}- (void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];}- (void)viewDidDisappear:(BOOL)animated{    [super viewDidDisappear:animated];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    // Return YES for supported orientations    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}- (BOOL) sendAudioToServer :(NSData *)data {    NSData *d = [NSData dataWithData:data];    //now you'll just have to send that NSData to your server    return YES;}-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{NSLog(@"stoped");    if (!stoped) {        NSData *data = [NSData dataWithContentsOfURL:recorder.url];        [self sendAudioToServer:data];        [recorder record];NSLog(@"stoped sent and restarted");    }}- (IBAction)startRec:(id)sender {if (!audioRecorder.recording){    sendButton.enabled = YES;    stopButton.enabled = YES;    [audioRecorder record];}}- (IBAction)sendToServer:(id)sender {stoped = NO;[audioRecorder stop];}- (IBAction)stop:(id)sender {stopButton.enabled = NO;sendButton.enabled = NO;recordButton.enabled = YES;stoped = YES;if (audioRecorder.recording){    [audioRecorder stop];}}@end

Good Luck.


It might actually be easier to have fixed-size chunks, instead of fixed-time. Then you can have two buffers, one that you currently fill with sample data. When the active buffer is full, then switch to fill the other buffer, while sending a signal to a sender-thread that takes the first buffer and sends it to the server.

You can of course use fixed-time instead, but then you need to make sure that the buffer is large enough to keep all samples, or use a dynamic buffer that can increase in size when needed. The double-buffering and sending thread can still be the same though.