如何将标题信息插入MPMoviePlayerController url? [英] How to insert header info into MPMoviePlayerController url?

查看:75
本文介绍了如何将标题信息插入MPMoviePlayerController url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用http协议实现视频流服务. 我知道如何将URL设置为MPMoviePlayerController,如何将headerField设置为NSMutableURLRequest,但是我不知道如何将它们组合在一起. 我实现了以下代码,但是无法正常工作,并且我认为是因为二进制数据中没有文件信息.

I need to implement video streaming service with http protocol. I know how to set url into MPMoviePlayerController, and how to set headerField into NSMutableURLRequest, but I have no idea how to combine them. I implement like below code, but not working, and I assume because there is no file info in the binary data.

- (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是必需的参数.

I confirmed there is data in the delegate method, but I don't know how to play it. Please somebody let me know how to play it. Auth-Token and Range are necessary parameters.

谢谢.

推荐答案

的确,Apple并未提供将标头注入MPMoviePlayerController请求的简便方法.稍作努力,您就可以使用自定义

It's true that Apple doesn't expose an easy way to inject headers into a MPMoviePlayerController's request. With a bit of effort, you can get this done using a custom NSURLProtocol. So, let's do it!

MyCustomURLProtocol.h:

MyCustomURLProtocol.h:

@interface MyCustomURLProtocol : NSURLProtocol <NSURLConnectionDelegate, NSURLConnectionDataDelegate>

@property (nonatomic, strong) NSURLConnection* connection;

@end

MyCustomURLProtocol.m:

MyCustomURLProtocol.m:

@implementation  MyCustomURLProtocol

// Define which protocols you want to handle
// In this case, I'm only handling "customProtocol" manually
// Everything else, (http, https, ftp, etc) is handled by the system
+ (BOOL) canInitWithRequest:(NSURLRequest *)request {
    NSURL* theURL = request.URL;
    NSString* scheme = theURL.scheme;
    if([scheme isEqualToString:@"customProtocol"]) {
        return YES;
    }
    return NO;
}

// You could modify the request here, but I'm doing my legwork in startLoading
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
    return request;
}

// I'm not doing any custom cache work
+ (BOOL) requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b {
    return [super requestIsCacheEquivalent:a toRequest:b];
}

// This is where I inject my header
// I take the handled request, add a header, and turn it back into http
// Then I fire it off
- (void) startLoading {
    NSMutableURLRequest* mutableRequest = [self.request mutableCopy];
    [mutableRequest setValue:@"customHeaderValue" forHTTPHeaderField:@"customHeaderField"];

    NSURL* newUrl = [[NSURL alloc] initWithScheme:@"http" host:[mutableRequest.URL host] path:[mutableRequest.URL path]];
    [mutableRequest setURL:newUrl];

    self.connection = [NSURLConnection connectionWithRequest:mutableRequest delegate:self];
}

- (void) stopLoading {
    [self.connection cancel];
}

// Below are boilerplate delegate implementations
// They are responsible for letting our client (the MPMovePlayerController) what happened

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [self.client URLProtocol:self didFailWithError:error];
    self.connection = nil;
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.client URLProtocol:self didLoadData:data];
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.client URLProtocolDidFinishLoading:self];
    self.connection = nil;
}

@end

在使用自定义URL协议之前,必须先注册它.在您的AppDelegate.m中:

Before you can use your custom URL Protocol, you must register it. In your AppDelegate.m:

#import "MyCustomURLProtocol.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // ... Your normal setup ...

    [NSURLProtocol registerClass:[MyCustomURLProtocol class]];

    return YES;
}

最后,您需要使用MPMediaPlayerController来使用自定义URL协议.

Finally, you need to user your custom URL Protocol with the MPMediaPlayerController.

NSString* theURLString = [NSString stringWithFormat:@"customProtocol://%@%@", [_url host],[_url path]];
_player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:theURLString]];

MPMoviePlayerController现在将尝试使用customProtocol://而不是普通的http://发出请求.使用此设置,我们然后拦截该请求,添加标头,将其转换为http,然后关闭所有内容.

The MPMoviePlayerController will now attempt to make the request with customProtocol:// instead of normal http://. Using this setup, we then intercept that request, add our headers, turn it into http, and then fire everything off.

这篇关于如何将标题信息插入MPMoviePlayerController url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆