Take the 2-minute tour ×

I need to implement video streaming service with http protocol. I know how to set url into MPMoviePlayerController, and how to set headerField into NSMutableURLRequest, but I have no idea how to combine them. I implement like below code, but not working, and I assume because there is no file info in the binary data.

- (void) openUrl
    NSMutableURLRequest *reqURL = [NSMutableURLRequest requestWithURL:
                               [NSURL URLWithString:@""] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];

    [reqURL setHTTPMethod:@"GET"];
    [reqURL setValue:@"Mozilla/4.0 (compatible;)" forHTTPHeaderField:@"User-Agent"];
    [reqURL setValue:@"AAA-bb" forHTTPHeaderField:@"Auth-Token"];
    [reqURL setValue:@"bytes=0-1024" forHTTPHeaderField:@"Range"];

    [NSURLConnection connectionWithRequest:reqURL delegate:self];


    NSError * jsonERR = nil;

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myMove.ts"];

    [data writeToFile:path atomically:YES];
    NSURL *moveUrl = [NSURL fileURLWithPath:path];

    MPMoviePlayerController *player = [[MPMoviePlayerController alloc]init];
    [player setContentURL:moveUrl];
    player.view.frame = self.view.bounds;
    player.controlStyle = MPMovieControlStyleEmbedded;
    [self.view addSubview:player.view];
    [player play];

I confirmed there is data in the delegate method, but I don't know how to play it. Please somebody let me know how to play it. Auth-Token and Range are necessary parameters.


share|improve this question
add comment

1 Answer

Go back and read the documentation for NSURLConnection, and the URL-loading system in general. -connection:didReceiveData: is likely called more than once, as each chunk of the file arrives. You need to handle that, rather than assuming only the complete data arrives all at once.

share|improve this answer
add comment

Your Answer


