2017-01-12 8 views
0

にビデオストリームを読み込むことができません私はWEBAPIからビデオをストリーミングしたい、私はiOSの開発に新しいです

、ファイルが認証を必要とする私たちのバックエンドサーバからストリーミングされています。これは、認証HTTPヘッダーでキーベースの認証セットです。

AVPlayerで試したところ、私の出力はありませんでした。いくつかのより多くの研究を行った後、私は

[NSURLProtocol registerClass:[MyCustomProtocol class]]; 

    NSString *theURLString = @"customProtocol://abcd.com/download"; 
    player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:theURLString]]; 
    [self.view addSubview:player.view]; 
    player.view.frame = self.view.frame; 

    [player play]; 

customProtocol.m、customProtocolはそれは私がcustomProtocolを使用して、このコードを試みたが、customProtocolが customProtocol.h呼ば取得されていないていない、より有用であろう見つかった一つが、ここで私を助けることができますか?どこで私は間違いをしていますか?

ありがとうございます!

これは私のcustomProtocolコードです:

@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; 
} 

答えて

1

あなたは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]; 

の下に使用することができます

+0

私はもう運がないことを試みた –

1
MPMoviePlayerViewController * movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; 

movieController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming; 
[self presentMoviePlayerViewControllerAnimated:movieController]; 
[movieController.moviePlayer play]; 
+0

こんにちはlalitあなたのご返信ありがとうございますが、それでもまだ動作していないプレーヤーが来て、すぐに解任します –

関連する問題