【问题标题】:Unable to load video stream in无法加载视频流
【发布时间】:2017-01-12 11:06:32
【问题描述】:

我是 iOS 开发新手,

我想从 webapi 流式传输视频,文件是从需要身份验证的后端服务器流式传输的。它是 Authorization HTTP Header 中基于密钥的身份验证集。

我尝试使用 AVPlayer 没有得到我的输出。在做了更多研究之后,我发现 customProtocol 会更有用,所以我使用了 customProtocol 并尝试使用此代码,但 customProtocol 没有被调用customProtocol.hcustomProtocol.m

    [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];

有人可以帮我吗?我哪里出错了?

提前致谢!

这是我的自定义协议代码:

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

【问题讨论】:

    标签: ios iphone ipad mpmovieplayercontroller avplayer


    【解决方案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];
    

    它可能有用...试试看

    【讨论】:

      【解决方案2】:
      MPMoviePlayerViewController * movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
      
      movieController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
      [self presentMoviePlayerViewControllerAnimated:movieController];
      [movieController.moviePlayer play];
      

      【讨论】:

      • 你好 lalit 谢谢你的热情回复,但它仍然无法正常工作,玩家来了又很快就解散了
      猜你喜欢
      • 1970-01-01
      • 2010-11-07
      • 2017-06-18
      • 2017-04-26
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多