在我的iOS应用程序中,我向Java Servlet发送一个GET请求,该请求成功地向我发送了一个.mp4视频。我正在使用NSURLSession API,我想在完成块中播放视频,但视频从不播放。我没有看到我的代码有什么问题:
@interface ViewVideoViewController ()
@property (nonatomic) NSURLSession *session;
@property (nonatomic, strong) MPMoviePlayerController *player;
@end
@implementation ViewVideoViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *requestedURL=[NSString stringWithFormat:@"http://urlservlet.com?filename=%@", self.filename];
NSURL *url = [NSURL URLWithString:requestedURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"GET"];
[request setURL:url];
NSURLSessionConfiguration *config =
[NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:config
delegate:nil
delegateQueue:nil];
NSURLSessionDownloadTask *downloadTask = [self.session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
NSData *downloadedData = [NSData dataWithContentsOfURL:location
options:kNilOptions
error:nil];
NSLog(@"Video size: %lu", (unsigned long)downloadedData.length);//the result is good so the video is here
self.player = [[MPMoviePlayerController alloc] initWithContentURL:location];
UIView *view = self.player.view;
view.frame = CGRectMake(16, 84, 288, 128);
[self.view addSubview:view];
[self.player prepareToPlay];
[self.player play];
}];
[downloadTask resume];
}
@end