ios - 可以使用 NSURLProtocol 的子类、使用 MPMovieController 或 AVFoundation 来播放视频吗?

标签 ios mpmovieplayercontroller avfoundation avplayer nsurlprotocol

我目前正在尝试将视频播放到具有在自定义 NSURLProtocol 子类中定义的自定义方案的 URL。最初我试图使用 MPMoviePlayerController 来实现这一点,但在遇到问题并检查堆栈溢出后,我发现 MPMoviePlayerController 没有按预期处理 NSURLProtocol 子类。

How to play movie with a URL using a custom NSURLProtocol?

因此我决定看一下 AVFoundation 框架,然而,这似乎也行不通。我只是想知道这是否可行,或者我是否在尝试穿墙?

使用 AVFoundation,我使用的方法如下所示。可能值得一提的是,这在使用标准 URL 指向托管在 Internet 上的视频时有效,但不适用于自定义 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,或者可能离线保存流,或者用猫视频等替换它......为此,你需要创建一个新类我的扩展 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 - 可以使用 NSURLProtocol 的子类、使用 MPMovieController 或 AVFoundation 来播放视频吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15269790/

相关文章:

ios - 设置 MPMoviePlayerController 的方向

ios - 如何在 iOS 中使用 AVFoundation 压缩视频

ios - AVFoundation中,如何同步录制和播放

IOS 为什么单元测试一个类调用 ViewController

ios - 哪些属性应该在 View Controller m 中声明,哪些在模型 m objective-c 中声明?

ios - 在后台播放 MPMoviePlayerController

ios - 修改 MPMoviePlayerViewController 上允许的方向(从 UIWebView 调用)。 iOS 5 和 iOS 6

iPhone:AVCaptureSession 捕获输出崩溃(AVCaptureVideoDataOutput)

iphone - 更改 UISegmentedControl 的布局?

c++ - 64 位架构的 iOS JUCE 库问题