Note that there are some explanatory texts on larger screens.

plurals
  1. POSending AVAudioRecorder to server iOS
    primarykey
    data
    text
    <p>I am trying for the past few days to upload a locally saved sounde record to a server (which handels it with a php file and saves it to the server). the problem is that I can't find a way to do that.</p> <p>While recording the sound (AVAudioRecorder) it saves at a "NSTemporaryDirectory()":</p> <pre><code>NSURL *temporaryRecFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"VoiceFile"]]]; </code></pre> <p>Now, everywhere I checked the answer was to use <a href="http://allseeing-i.com/ASIHTTPRequest/" rel="nofollow">ASIHTTPRequest</a>. but the problem is that it is not supported in ios6.</p> <p>There are some other libraries for it but nothing works no matter what and how I try. I would love it if someone could help me...</p> <p><em>Here is my code:</em></p> <p><strong>ViewController.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import &lt;AVFoundation/AVFoundation.h&gt; #import &lt;CoreAudio/CoreAudioTypes.h&gt; @interface ViewController : UIViewController &lt;AVAudioRecorderDelegate,AVAudioPlayerDelegate&gt; { UIButton *recordButton; UIButton *playButton; UIButton *uploadFile; UILabel *recStateLabel; BOOL isNotRecording; NSURL *temporaryRecFile; AVAudioRecorder *recorder; AVAudioPlayer *player; } @property (nonatomic,retain) IBOutlet UIButton *uploadFile; @property (nonatomic,retain) IBOutlet UIButton *playButton; @property (nonatomic,retain) IBOutlet UIButton *recordButton; @property (nonatomic,retain) IBOutlet UILabel *recStateLabel; -(IBAction)recording; -(IBAction)playback; -(IBAction)upload; @end </code></pre> <p><strong>ViewController.m</strong></p> <pre><code>#import "ViewController.h" #import &lt;AVFoundation/AVFoundation.h&gt; #import "STHTTPRequest.h" @interface ViewController () @end @implementation ViewController @synthesize recordButton,playButton,uploadFile,recStateLabel; -(IBAction)recording { if(isNotRecording) { isNotRecording = NO; [recordButton setTitle:@"Stop" forState:UIControlStateNormal]; playButton.hidden = YES; recStateLabel.text = @"Recording"; temporaryRecFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"VoiceFile"]]]; NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, [NSNumber numberWithInt:16], AVEncoderBitRateKey, [NSNumber numberWithInt: 2], AVNumberOfChannelsKey, [NSNumber numberWithFloat:44100.0], AVSampleRateKey, nil]; recorder = [[AVAudioRecorder alloc]initWithURL:temporaryRecFile settings:recordSettings error:nil]; [recorder setDelegate:self]; [recorder prepareToRecord]; [recorder record]; } else { isNotRecording = YES; [recordButton setTitle:@"Rec" forState:UIControlStateNormal]; playButton.hidden = NO; recStateLabel.text = @"Not Recording"; [recorder stop]; } } -(IBAction)playback { player = [[AVAudioPlayer alloc]initWithContentsOfURL:temporaryRecFile error:nil]; //player.volume = 1; [player setVolume: 1.0]; [player play]; } -(IBAction)upload { NSLog(@"upload"); recStateLabel.text = @"Uploading"; NSData *data = [NSData dataWithContentsOfFile:NSTemporaryDirectory()]; NSMutableString *urlString = [[NSMutableString alloc] initWithFormat:@""]; [urlString appendFormat:@"%@", data]; NSData *postData = [urlString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSString *baseurl = @"http://efron.org.il/dev/singsong/upload.php"; NSURL *url = [NSURL URLWithString:baseurl]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setHTTPMethod: @"POST"]; [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [urlRequest setHTTPBody:postData]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self]; [connection start]; NSLog(@"Started!"); /* STHTTPRequest *r = [STHTTPRequest requestWithURLString:@"http://efron.org.il/dev/singsong/upload.php"]; r.completionBlock = ^(NSDictionary *headers, NSString *body) { // ... }; r.errorBlock = ^(NSError *error) { NSLog(@"error"); }; [r setFileToUpload:NSTemporaryDirectory() parameterName:@"sound"]; [r startAsynchronous]; ///................../// NSString *boundary = @"---------------------------14737809831466499882746641449"; NSMutableData *postData = [NSMutableData data]; NSString *header = [NSString stringWithFormat:@"--%@\r\n", boundary]; [postData appendData:[header dataUsingEncoding:NSUTF8StringEncoding]]; //add your filename entry NSString *contentDisposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", @"filename", @"iosaudio"]; [postData appendData:[contentDisposition dataUsingEncoding:NSUTF8StringEncoding]]; [postData appendData:[NSData dataWithContentsOfFile:NSTemporaryDirectory()]]; NSString *endItemBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n",boundary]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setHTTPMethod:@"POST"]; [request setURL:[NSURL URLWithString:@"http://efron.org.il/dev/singsong/upload.php"]]; [postData appendData:[endItemBoundary dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:postData]; ///.........../// NSURL *url = [NSURL URLWithString:@"http://efron.org.il/"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; NSData *songData = [NSData dataWithContentsOfURL:temporaryRecFile]; NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"dev/singsong/upload.php" parameters:nil constructingBodyWithBlock: ^(id &lt;AFMultipartFormData&gt;formData) { [formData appendPartWithFormData:songData name:@"file"]; }]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { recStateLabel.text = [NSString stringWithFormat:@"Sent %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite]; }]; [httpClient enqueueHTTPRequestOperation:operation]; */ } - (void)viewDidLoad { isNotRecording = YES; playButton.hidden = YES; recStateLabel.text = @"Not Recording"; AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; [audioSession setActive:YES error:nil]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end </code></pre> <p>The <strong>ViewController.m</strong> includes some test I have made with libraries such as <a href="https://github.com/nst/STHTTPRequest" rel="nofollow">STHTTPRequest</a> and AFNetworking</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload