This is my h5 code:
<video id="myVideo" src="xxxapp://***.***.xx/***/***.mp4" style="object-fit:cover;opacity:1;width:100%;height:100%;display:block;possition:absolute;" type="video/mp4"></video>
I want to load local large video, so, I use WKURLSchemeHandler.
- (void)webView:(WKWebView *)webView startURLSchemeTask:(id<WKURLSchemeTask>)urlSchemeTask {
NSURLRequest *request = [urlSchemeTask request];
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"***" ofType:@"mp4"];
NSData *videoData = [NSData dataWithContentsOfFile:videoPath options:nil error:nil];
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:url MIMEType:@"video/mp4" expectedContentLength:videoData.length textEncodingName:nil];
[urlSchemeTask didReceiveResponse:response];
[urlSchemeTask didReceiveData:videoData];
[urlSchemeTask didFinish];
}
but its not work, data is not nil, but video do not play. I would greatly appreciate it if someone could help me find a solution!!
ps: <video><source/></video> can make it, but we cannot use it due to some reasons.
Finished by https 206 code
NSString *oldRange = urlSchemeTask.request.allHTTPHeaderFields[@"Range"];
oldRange = [oldRange substringFromIndex:6];
NSArray *array = [oldRange componentsSeparatedByString:@"-"];
NSInteger first = [[array firstObject] intValue];
NSInteger last = [[array lastObject] intValue];
NSLog(@"====> startURLSchemeTask2 response oldRange %@", oldRange);
NSRange newVideoRange = NSMakeRange(first, last - first + 1);
NSData *newVideoData = [videoData subdataWithRange:newVideoRange];
// 构造响应
NSError *error = nil;
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:requestURL
statusCode:206
HTTPVersion:nil
headerFields:@{
@"Content-Range": [NSString stringWithFormat:@"bytes %lu-%lu/%lu", (unsigned long)first, (unsigned long)last, (unsigned long)videoData.length],
@"Content-Type": @"video/mp4",
@"Content-Length": [NSString stringWithFormat:@"%@", @(last - first + 1)],
@"Access-Control-Allow-Origin": @"*"
}];