0

POSTリクエストを介して、iPhoneからサーバーにオーディオデータを送信しようとしています。ファイルを使用したくありませんが、サーバーにデータを直接送信します。AudioToolbox を使用してマイクからバッファにデータを取得し、このバッファをサーバーに送信します。データは正しく受信され、サーバーは wave ファイルを作成します...しかし、このファイルが空でなくても、ファイルのデュレーションが短すぎる場合のように、サウンドを再生できません...

ポスト リクエストを送信するコールバック関数のコードは次のとおりです。

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);

}
4

0 に答える 0