我需要使用 http 协议实现视频流服务。我知道如何将 url 设置为 MPMoviePlayerController,以及如何将 headerField 设置为 NSMutableURLRequest,但我不知道如何组合它们。我像下面的代码一样实现,但不工作,我假设是因为二进制数据中没有文件信息。
- (void) openUrl
{
NSMutableURLRequest *reqURL = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:@"http://111.222.33.44/MOV/2013/4/123123123"] 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"];
[reqURL setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection connectionWithRequest:reqURL delegate:self];
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"Received");
NSError * jsonERR = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myMove.ts"];
[data writeToFile:path atomically:YES];
NSLog(@"copied");
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];
}
我确认委托方法中有数据,但不知道怎么玩。请有人告诉我怎么玩。Auth-Token 和 Range 是必要的参数。
谢谢。