Upload audio - http streaming

会有一股神秘感。 提交于 2019-12-04 20:20:08

The solution is create an subclass of NSInputStream, and implement the methods open, close, read, hasBytesAvailable and don't forget - (NSStreamStatus)streamStatus. Last method is invoked from http to know if we are open, close or we was finished (NSStreamStatusAtEnd) to send (there are others status, but this are the most important). I use a tmp file like buffer because I have to send a lot of data, but, perhaps, a data memory buffer could be better. Finally I implement other class where use my custom 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]; 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!