Ios 无法在中加载视频流
我不熟悉iOS开发 我想从webapi流式传输视频,这些文件是从我们的后端服务器流式传输的,这需要身份验证。它是授权HTTP头中基于密钥的身份验证集 我试过使用AVPlayer,但没有得到输出。在做了更多的研究之后,我发现customProtocol会更有用,所以我使用了customProtocol并尝试使用此代码,但customProtocol没有被调用 有人能帮我吗?我在哪里犯了错误 提前感谢您强> 这是我的customProtocol代码:Ios 无法在中加载视频流,ios,iphone,ipad,mpmovieplayercontroller,avplayer,Ios,Iphone,Ipad,Mpmovieplayercontroller,Avplayer,我不熟悉iOS开发 我想从webapi流式传输视频,这些文件是从我们的后端服务器流式传输的,这需要身份验证。它是授权HTTP头中基于密钥的身份验证集 我试过使用AVPlayer,但没有得到输出。在做了更多的研究之后,我发现customProtocol会更有用,所以我使用了customProtocol并尝试使用此代码,但customProtocol没有被调用 有人能帮我吗?我在哪里犯了错误 提前感谢您 这是我的customProtocol代码: @implementation MyCustomPr
@implementation MyCustomProtocol
+ (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];
Constants *constants = [Constants sharedInstance];
[mutableRequest setValue:[NSString stringWithFormat:@"Bearer %@",constants.access_token] forHTTPHeaderField:@"Authorization"];
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;
}
如果您通过了带有url的身份验证标头,则可以使用以下代码
NSMutableDictionary * headers = [NSMutableDictionary dictionary];
[headers setObject:@"Your UA" forKey:@"User-Agent"];
AVURLAsset * asset = [AVURLAsset URLAssetWithURL:URL options:@{@"AVURLAssetHTTPHeaderFieldsKey" : headers}];
AVPlayerItem * item = [AVPlayerItem playerItemWithAsset:asset];
self.player = [[AVPlayer alloc] initWithPlayerItem:item];
它可能会起作用…试试看如果您已经通过了带有url的身份验证头,那么您可以使用下面的代码
NSMutableDictionary * headers = [NSMutableDictionary dictionary];
[headers setObject:@"Your UA" forKey:@"User-Agent"];
AVURLAsset * asset = [AVURLAsset URLAssetWithURL:URL options:@{@"AVURLAssetHTTPHeaderFieldsKey" : headers}];
AVPlayerItem * item = [AVPlayerItem playerItemWithAsset:asset];
self.player = [[AVPlayer alloc] initWithPlayerItem:item];
这可能有用…试试看你好lalit谢谢你的友好回复但仍然不起作用玩家来了并迅速解散你好lalit谢谢你的友好回复但仍然不起作用玩家来了并迅速解散
NSMutableDictionary * headers = [NSMutableDictionary dictionary];
[headers setObject:@"Your UA" forKey:@"User-Agent"];
AVURLAsset * asset = [AVURLAsset URLAssetWithURL:URL options:@{@"AVURLAssetHTTPHeaderFieldsKey" : headers}];
AVPlayerItem * item = [AVPlayerItem playerItemWithAsset:asset];
self.player = [[AVPlayer alloc] initWithPlayerItem:item];