Download HTTP Audio Streaming

I am trying to download a linear file under http in streaming mode. The idea is to take these steps at the same time, 1) Topic 1: record the audio file and save it in a temporary file 2) Thread 2: Take n bytes from the temporary file and send it to the http server.

How can I write an http stream ?, In CFHTTPStream I did not see the writing methods, I just read: s Do I need to use sockets? Thanks!!!

My actual code

CFWriteStreamRef stream; NSString *strUrl = @"myurl"; NSURL *url = [[[NSURL alloc] initWithString:strUrl] retain]; CFStringRef requestMethod = CFSTR("GET"); CFHTTPMessageRef message= CFHTTPMessageCreateRequest(kCFAllocatorDefault, requestMethod, (CFURLRef)url, kCFHTTPVersion1_1); CFHTTPMessageSetHeaderFieldValue(message, CFSTR("Content-Type"), CFSTR("multipart/form-data")); stream = ?? //CFReadStreamCreateForHTTPRequest(NULL, message); CFRelease(message); //other headers... if (CFWriteStreamSetProperty(stream, kCFStreamPropertyHTTPShouldAutoredirect, kCFBooleanTrue) == false) { NSLog(@"Error"); return NO; } // // Open the stream // if (!CFWriteStreamOpen(stream)) { CFRelease(stream); NSLog(@"Error"); return NO; } CFStreamClientContext context = {0, self, NULL, NULL, NULL}; CFWriteStreamSetClient(stram, kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered, RSWriteStreamCallBack, &context); CFWriteStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); 
+1
source share
1 answer

The solution is a subclass of NSInputStream and implements the open, close, read, hasBytesAvailable methods and does not forget - (NSStreamStatus) streamStatus. The last method is called from http to find out whether we are open, close, or we were completed (NSStreamStatusAtEnd) for sending (there are other statuses, but these are the most important). I use the tmp file as a buffer because I need to send a lot of data, but maybe a data memory buffer might be better. Finally, I am implementing another class where they use their own NSInputStream, here is the code:

  NSURL *url = [NSURL URLWithString:@"url"]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; [req setHTTPMethod:@"POST"]; //set headers if you have to do for example: NSString *contentType = [NSString stringWithFormat:@"multipart/form-data"]; [req setValue:contentType forHTTPHeaderField:@"Content-Type"]; //Create your own InputStream instream = [[CustomStream alloc] init]; [req setHTTPBodyStream:instream]; //I remove instream later NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:NO]; [aConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [aConnection start]; 
+3
source

All Articles