iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了xcode – 如何将标题信息插入MPMoviePlayerController网址?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

需要用http协议实现视频流服务. 我知道如何将url设置为MPMoviePlayerController,以及如何将headerField设置为NSMutableURLrequest,但我不知道如何组合它们. 我实现如下代码,但不工作,我假设因为二进制数据中没有文件信息. - (void) openUrl { NSMutableURLrequest *requRL = [NSMutab
需要用http协议实现视频流服务.
我知道如何将url设置为MPMoviePlayerController,以及如何将headerField设置为NSMutableURLrequest,但我不知道如何组合它们.
我实现如下代码,但不工作,我假设因为二进制数据中没有文件信息.

- (void) openUrl
{
    NSMutableURLrequest *requRL = [NSMutableURLrequest requestWithURL:
                               [NSURL URLWithString:@"http://111.222.33.44/MOV/2013/4/123123123"] cachePolicy:NSURLrequestuseProtocolCachePolicy timeoutInterval:30.0];

    [requRL sethttpR_510_11845@ethod:@"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是必要的参数.

谢谢.

解决方法

Apple确实没有公开一种将标头注入MPMoviePlayerController请求的简单方法.通过一些努力,您可以使用自定义 NSURLProtocol完成此操作.所以,让我们这样做!

@H_145_21@myCustomURLProtocol.h:

@interface MyCustomURLProtocol : NSURLProtocol <NSURLConnectionDelegate,NSURLConnectionDataDelegate>

@property (nonatomic,strong) NSURLConnection* connection;

@end
@H_145_21@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* scheR_510_11845@e = theURl.scheR_510_11845@e;
    if([scheR_510_11845@e 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] initWithscheR_510_11845@e:@"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中:

#import "MyCustomURLProtocol.h"

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

    // ... Your normal setup ...

    [NSURLProtocol registerClass:[MyCustomURLProtocol class]];

    return YES;
}

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

NSString* theURLString = [NSString StringWithFormat:@"customProtocol://%@%@",[_url host],[_url path]];
_player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:theURLString]];
@H_145_21@mPMoviePlayerController现在将尝试使用customProtocol://而不是正常的http://来发出请求.使用此设置,我们然后拦截该请求,添加我们的标头,将其转换为http,然后关闭所有内容.

大佬总结

以上是大佬教程为你收集整理的xcode – 如何将标题信息插入MPMoviePlayerController网址?全部内容,希望文章能够帮你解决xcode – 如何将标题信息插入MPMoviePlayerController网址?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。