AVPlayer / AVPlayerItem如何通知我的应用程序无法访问网络故障? [英] How does AVPlayer / AVPlayerItem inform my application about unreachable network failure?

查看:139
本文介绍了AVPlayer / AVPlayerItem如何通知我的应用程序无法访问网络故障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AVPlayer 在iOS应用程序中实现自定义视频播放器。要从网络播放视频我分配了一个播放器:

I’m using AVPlayer to implement a custom video player in an iOS application. To play video from the network I allocate a player:

[[AVPlayer alloc] initWithURL:_URL];

创建资产:

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.URL
options:@{AVURLAssetPreferPreciseDurationAndTimingKey : @(YES)}];

以异步方式加载可播放的键:

NSArray *keys = @[@"playable"];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^{
        for (NSString *key in keys) {
            NSError *error;
            AVKeyValueStatus status = [asset statusOfValueForKey:key error:&error];

            if (status == AVKeyValueStatusFailed) {
                NSLog(@"Failed to load asset key %@ error %@", key, [error localizedDescription]);
                [self fail];
                return;
            }
        }

        if (asset.isPlayable) {
            [self.player replaceCurrentItemWithPlayerItem:self.playerItem];
        }
        else {
            [self fail];
        }
    });
}];

问题是当设备没有互联网连接或高丢包时, completionHandler 永远不会被调用(即使在等待几分钟后),所以我不知道何时向用户显示加载视频失败的消息。

The problem is that when the device has no internet connection or high packet loss, the completionHandler is never called (even after waiting for minutes) and so I have no idea when to show a message to the user that loading the video failed.

我的问题是:


  1. 我错过了什么/有没有更好的方法来解决这个问题?

  2. 我应该滚动自己的超时逻辑,还是只是 loadValuesAsynchronouslyForKeys:可达性认为无法访问网络。

  3. 有关 AVPlayer 和网络故障的最佳做法是什么?

  1. Am I missing something / is there a better way to handle this?
  2. Should I roll my own timeout logic or should I just never loadValuesAsynchronouslyForKeys: when Reachability believes that the network isn't reachable.
  3. What are the best practices regarding AVPlayer and network failures?


推荐答案

事实证明这可能与我测试的方式有关。使用Network Link调节器没有触发阻塞,但是当我使用Charles Proxy实际阻止主机时,一切都按预期工作。我现在正试着使用我原来的代码,并且如果在现实世界测试期间证明它仍然存在问题,将会添加一些Dan的想法

It turns out that this may have been related to the way I was testing. Using the Network Link conditioner didn't trigger the block, but when I actually blocked the host using Charles Proxy, everything worked as intended. I'm trying to go with my original code for now, and will add in some of Dan's ideas if it proves to be problematic still during real world testing

这篇关于AVPlayer / AVPlayerItem如何通知我的应用程序无法访问网络故障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆