Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am developing an iOS app that has a button with a microphone on it (along with other features). When the user presses the microphone, it gets highlighted and the app should now start recording sound from the device´s microphone and send to a server (a server dedicated to the app, developed by people that I know, so I can affect its design).

I am looking for the simplest yet sturdiest approach to do this, i.e. I have no need to develop a complicated streaming solution or VoIP functionality, unless it is as simple to do as anything else.

The main problem is that we have no idea for how long the user will be recording sound, but we want to make sure that sounds are sent to the server continuously, we do not wish to wait until the user has finished recording. It is okay if the data arrives to the server in chunks however we do not wish to miss any information that the user may be recording, so one chunk must continue where the previous one ended and so on.

Our first thought was to create "chunks" of sound clips of for example 10 seconds and send them continuously to the server. Is there any streaming solution that is better/simpler that I am missing out on?

My question is, what would be the most simple but still reliable approach on solving this task on iOS?

Is there a way to extract chunks of sound from a running recording by AVAudioRecorder, without actually stopping the recording?

like image 680
jake_hetfield Avatar asked Jul 27 '12 11:07

jake_hetfield


People also ask

Can I record streaming audio on my iPhone?

Well, iPhone users can take advantage of their in-built Voice Memo app to capture streaming audio on their devices. It's free and easy to use. The app even lets you edit the recording. And if you want advanced features, then you can get the recorder app from the App Store.

How can I remotely record on my iPhone?

On your phone, go to Settings and then Accessibility and then Voice Control. Turn on the switch for Voice Control. Then open the Camera app and line up your shot. Say “Turn up the volume” or “Turn down the volume,” and the shutter will be triggered instead.


1 Answers

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.

like image 95
Mehdi Avatar answered Nov 01 '22 14:11

Mehdi