http 媒体流通过 https 通过 MVMoviePlayerViewController for iphone

发布于 2025-01-08 01:01:15 字数 3308 浏览 0 评论 0原文

我想通过MVMoviePlayerViewController实现通过https进行http媒体流的功能。我尝试通过 NSURLConnection 访问 https 服务器,然后通过 MVMoviePlayerViewController 打开媒体。我可以在模拟器和设备下通过此方法成功通过 UIWebView 打开图像 url,并在模拟器下通过 MVMoviePlayerViewController 打开 mp3 和 mp4 等媒体 url。奇怪的是,我无法通过设备下的 MVMoviePlayerViewController 打开媒体 url。以下是错误日志。

AVPlayerItem 类的实例 0x4b4650 已被释放,而键值观察者仍向其注册。观察信息被泄露,甚至可能被错误地附加到其他物体上。在 NSKVODallocateBreak 上设置断点以在调试器中停止。以下是当前的观测信息: ( 上下文:0x0,属性:0x208c20>上下文:0x0,属性:0xa8626b0>

以下是我的一些代码。

-(void)viewDidLoad 
{
    NSString *stringPath = [NSString stringWithFormat:@"https://xxxxx/xxx.mp4";
    NSURL *urlPath = [NSURL URLWithString:[stringPath stringByAddingPercentEscapesUsingEncoding:NSUTF8St ringEncoding]];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlPath];
    NSURLConnection *httpConnection = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
}

- (BOOL)connection : (NSURLConnection *)connection canAuthenticateAgainstProtectionSpace : (NSURLProtectionSpace *)protectionSpace 
{
    return YES;
}

- (void)connection : (NSURLConnection *)connection didReceiveAuthenticationChallenge : (NSURLAuthenticationChallenge *)challenge 
{ 
    if ([challenge previousFailureCount] > 0) 
    {
        return;
    }

    NSString *authenticationMethod = challenge.protectionSpace.authenticationMethod;
    if ([authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTru st])
    {
        SecTrustRef secTrustRef = challenge.protectionSpace.serverTrust;
        [challenge.sender useCredential:[NSURLCredential credentialForTrust:secTrustRef forAuthenticationChallenge:challenge];
    } 
    else 
    {
        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"adminr" password:@"admin" persistence:NSURLCredentialPersistenceForSession];
        [challenge.sender useCredential:newCredential forAuthenticationChallenge:challenge];
    }
}

- (void)connection : (NSURLConnection *)connection didReceiveResponse : (NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    assert([httpResponse isKindOfClass:[NSHTTPURLResponse class]]);

    NSLog(@"didReceiveResponse, status code : %d", httpResponse.statusCode);

    NSString *stringPath = [NSString stringWithFormat:@"%@", testPath];
    NSURL *urlPath = [NSURL URLWithString:[stringPath stringByAddingPercentEscapesUsingEncoding:NSUTF8St ringEncoding]];

    if ((httpResponse.statusCode / 100) == 2) 
    {
        MPMoviePlayerViewController *movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:urlPath];
        [self presentMoviePlayerViewControllerAnimated:movieView Controller];
    } 
}

重现步骤

  1. 使用 NSURLConnection API 连接到不受信任的认证 https 服务器。

  2. 我将收到didReceiveAuthenticationChallenge(NSURLConnection委托)并获取NSURLAuthenticationMethodServerTrustauthenticationMethod。我使用 SecTrustRef 和 NSURLCredential 来访问不受信任的认证 https 服务器。

  3. 我将收到didReceiveAuthenticationChallenge(NSURLConnection委托)并使用NSURLCredential设置用户名和密码以访问不受信任的认证https服务器。

  4. 我都收到 didReceiveResponse (NSURLConnection delegate) 并获得 http 响应 statusCode 200 OK。

  5. 我尝试通过 MPMoviePlayerViewController API 打开 https 媒体 url。

I want to implement a function of http media streaming over https by MVMoviePlayerViewController. I try to access https server by NSURLConnection then open media by MVMoviePlayerViewController. I can successfully open the image url by UIWebView through this method under simulator and device and open media url like mp3 and mp4 by MVMoviePlayerViewController under simulator. The strange thing is that I get failed to open media url by MVMoviePlayerViewController under device. The following are error log.

An instance 0x4b4650 of class AVPlayerItem was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
(
Context: 0x0, Property: 0x208c20> Context: 0x0, Property: 0xa8626b0>

The following are some of my code.

-(void)viewDidLoad 
{
    NSString *stringPath = [NSString stringWithFormat:@"https://xxxxx/xxx.mp4";
    NSURL *urlPath = [NSURL URLWithString:[stringPath stringByAddingPercentEscapesUsingEncoding:NSUTF8St ringEncoding]];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlPath];
    NSURLConnection *httpConnection = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
}

- (BOOL)connection : (NSURLConnection *)connection canAuthenticateAgainstProtectionSpace : (NSURLProtectionSpace *)protectionSpace 
{
    return YES;
}

- (void)connection : (NSURLConnection *)connection didReceiveAuthenticationChallenge : (NSURLAuthenticationChallenge *)challenge 
{ 
    if ([challenge previousFailureCount] > 0) 
    {
        return;
    }

    NSString *authenticationMethod = challenge.protectionSpace.authenticationMethod;
    if ([authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTru st])
    {
        SecTrustRef secTrustRef = challenge.protectionSpace.serverTrust;
        [challenge.sender useCredential:[NSURLCredential credentialForTrust:secTrustRef forAuthenticationChallenge:challenge];
    } 
    else 
    {
        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"adminr" password:@"admin" persistence:NSURLCredentialPersistenceForSession];
        [challenge.sender useCredential:newCredential forAuthenticationChallenge:challenge];
    }
}

- (void)connection : (NSURLConnection *)connection didReceiveResponse : (NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    assert([httpResponse isKindOfClass:[NSHTTPURLResponse class]]);

    NSLog(@"didReceiveResponse, status code : %d", httpResponse.statusCode);

    NSString *stringPath = [NSString stringWithFormat:@"%@", testPath];
    NSURL *urlPath = [NSURL URLWithString:[stringPath stringByAddingPercentEscapesUsingEncoding:NSUTF8St ringEncoding]];

    if ((httpResponse.statusCode / 100) == 2) 
    {
        MPMoviePlayerViewController *movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:urlPath];
        [self presentMoviePlayerViewControllerAnimated:movieView Controller];
    } 
}

STEPS TO REPRODUCE

  1. using NSURLConnection API to connect to an untrusted certification https server.

  2. I will receive didReceiveAuthenticationChallenge (NSURLConnection delegate) and get NSURLAuthenticationMethodServerTrust authenticationMethod. I use SecTrustRef and NSURLCredential to access to untrusted certification https server.

  3. I will receive didReceiveAuthenticationChallenge (NSURLConnection delegate) and use NSURLCredential to set username and password to access to untrusted certification https server.

  4. I all receive didReceiveResponse (NSURLConnection delegate) and get http response statusCode 200 OK.

  5. I try to open https media url by MPMoviePlayerViewController API.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文