0

I'm trying to send audio datas from iphone to a server, via a POST request. I don't want to use a file, but directly send datas to the server. I use AudioToolbox to get datas from microphone into a buffer, then i send this buffer to a server. Datas are correctly received, and the server create a wave file...but even if this file is not empty, no sound can be played..like if the duration of the file were too short...

Here is the code of the callback function, which send post request :

void AudioInputCallback(
                    void *inUserData,
                    AudioQueueRef inAQ,
                    AudioQueueBufferRef inBuffer,
                    const AudioTimeStamp *inStartTime,
                    UInt32 inNumberPacketDescriptions,
                    const AudioStreamPacketDescription *inPacketDescs) {

RecordState* recordState = (RecordState*)inUserData;    



    // setting up the URL to post to
    NSString *urlString = @"http://localhost/script/upload.php";


    // setting up the request object now
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*
     now lets create the body of the post
     */
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"test.wav\"\r\n",i] dataUsingEncoding:NSUTF8StringEncoding]];


    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  //NSUTF8StringEncoding

    [body appendData:[NSData dataWithBytes:inBuffer->mAudioData length:inBuffer->mAudioDataByteSize]];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // now lets make the connection to the web
    NSHTTPURLResponse *response = nil;
    NSError * error = nil;

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    AudioQueueStop(recordState->queue, true);

    AudioQueueFreeBuffer(recordState->queue,
                         recordState->buffers[0]);


    AudioQueueDispose(recordState->queue, true);

}
bytheway
  • 11
  • 2
  • Try to include a correct file into the app bundle and send it with your request, to be honest i don't understand it's structure completely so that's what i would do first . – A-Live May 28 '12 at 03:02
  • I tried to use AudioFileWritePackets to write datas in a file, then i send it throught the same post request and it worked. – bytheway May 28 '12 at 18:07
  • That's good, post it as an answer. – A-Live May 28 '12 at 18:12

0 Answers0