我目前正在尝试将视频播放到具有在自定义NSURLProtocol子类中定义的自定义方案的URL。最初,我尝试使用MPMoviePlayerController来完成此任务,但是在遇到问题并检查堆栈溢出后,我发现MPMoviePlayerController不能按预期处理NSURLProtocol子类。
How to play movie with a URL using a custom NSURLProtocol?
结果,我决定查看AVFoundation框架,但是,这似乎也不起作用。我只是想知道这是否可行,还是我要穿过墙壁?
使用AVFoundation,我正在使用的方法如下所示。值得一提的是,当使用标准网址访问互联网上托管的视频时,此方法有效,但不适用于自定义NSURLProtocol。
// this doesn't work
//AVPlayer *player = [[AVPlayer alloc] initWithURL:[NSURL urlWithString:@"custom URL scheme"]];
// this works
AVPlayer *player = [[AVPlayer alloc] initWithURL:[NSURL urlWithString:@"some url to video on remote server"]];
AVPlayerLayer *layer = [AVAVPlayerLayer playerLayerWithPlayer:player];
// configure the layer
[self.view.layer addSublayer:layer];
[player play];
为了从定义的NSURLProtocol子类播放,是否需要做些其他的事情?
最佳答案
最近,我设法使NSURLProtocol与MPMoviePlayerController一起使用。这主要有用,因为MPMoviePlayerController仅接受NSURL,因此它不允许我们传递cookie或 header (用于身份验证)。使用NSURLProtocol可以做到这一点。我以为我会在这里一次为社区共享一些代码。
基本上,通过使用NSURLProtocol,我们可以拦截MPMoviePlayerController和流请求之间的通信,沿途注入(inject)cookie,或可能将流离线保存,或将其替换为cat video等。为此,您需要创建一个新的类,我扩展的NSURLProtocol:
MyURLProtocol.h:
#import <Foundation/Foundation.h>
@interface MyURLProtocol : NSURLProtocol
+ (void) register;
+ (void) injectURL:(NSString*) urlString cookie:(NSString*)cookie;
@end
MyURLProtocol.m:
#import "MyURLProtocol.h"
@interface MyURLProtocol() <NSURLConnectionDelegate> {
NSMutableURLRequest* myRequest;
NSURLConnection * connection;
}
@end
static NSString* injectedURL = nil;
static NSString* myCookie = nil;
@implementation MyURLProtocol
// register the class to intercept all HTTP calls
+ (void) register
{
[NSURLProtocol registerClass:[self class]];
}
// public static function to call when injecting a cookie
+ (void) injectURL:(NSString*) urlString cookie:(NSString*)cookie
{
injectedURL = urlString;
myCookie = cookie;
}
// decide whether or not the call should be intercepted
+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
if([[[request allHTTPHeaderFields] objectForKey:@"BANANA"] isEqualToString:@"DELICIOUS"])
{
return NO;
}
return [[[request URL] absoluteString] isEqualToString:injectedURL];
}
// required (don't know what this means)
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
return request;
}
// intercept the request and handle it yourself
- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id<NSURLProtocolClient>)client {
if (self = [super initWithRequest:request cachedResponse:cachedResponse client:client]) {
myRequest = request.mutableCopy;
[myRequest setValue:@"DELICIOUS" forHTTPHeaderField:@"BANANA"]; // add your own signature to the request
}
return self;
}
// load the request
- (void)startLoading {
// inject your cookie
[myRequest setValue:myCookie forHTTPHeaderField:@"Cookie"];
connection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self];
}
// overload didReceive data
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[[self client] URLProtocol:self didLoadData:data];
}
// overload didReceiveResponse
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response {
[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:[myRequest cachePolicy]];
}
// overload didFinishLoading
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[[self client] URLProtocolDidFinishLoading:self];
}
// overload didFail
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[[self client] URLProtocol:self didFailWithError:error];
}
// handle load cancelation
- (void)stopLoading {
[connection cancel];
}
@end
用法:
[MyURLProtocol register];
[MyURLProtocol injectURL:@"http://server/your-video-url.mp4" cookie:@"mycookie=123"];
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:@"http://server/your-video-url.mp4"];
[moviePlayer play];
关于ios - 是否可以使用MPMovieController或AVFoundation使用NSURLProtocol的子类播放视频?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15269790/