播放器缓冲界面显示加载速度

由于其他APP中下载的速度可以根据请求的数据得到实际大小,但IOS中的播放器类的下载情况我们无法得到,除非自己对HLS中每个TS都主动下载。在这种情况下,可以通过监视网卡的速度来粗略的显示当前播放器的缓冲速度。

-(void)processSecSpeedText
{
    if (self.hidden == YES)
    {
        //[freshTimer invalidate];
    }
    long long int curdeviceNetBytes = 0;
    Reachability *CurReach = [Reachability reachabilityForInternetConnection];
    switch ([CurReach currentReachabilityStatus]) {
        case NotReachable://没有网络
        {
             NSLog(@"noActiveNet");
            break;
        }
        case ReachableViaWiFi://有wifi
        {
            curdeviceNetBytes = [self getWifiBytes];
            break;
        }
        case ReachableViaWWAN://有3G
        {
            curdeviceNetBytes = [self getGprsBytes];
            break;
        }
        default:
            break;
    }
    
    if (deviceNetBytes == 0)
    {
        deviceNetBytes = curdeviceNetBytes;
    }
    
    long long int deviceNetBytesGap = curdeviceNetBytes - deviceNetBytes;
    deviceNetBytes = curdeviceNetBytes;
    NSString* teststr = bytesToSpeedStr(deviceNetBytesGap);
    LoadingSpeed.text = teststr;
    
    CGSize textSize = [LoadingSpeed.text sizeWithFont:LoadingSpeed.font];
    
    LoadingSpeed.frame = CGRectMake(self.center.x-(textSize.width)/2, self.center.y-(textSize.height)/2, textSize.width, textSize.height);// self.center;
    
    
}

NSString *bytesToSpeedStr(long long int bytes)
{
    if(bytes < 1024)     // B
    {
        return [NSString stringWithFormat:@"%lldB/s", bytes];
    }
    else if(bytes >= 1024 && bytes < 1024 * 1024) // KB
    {
        return [NSString stringWithFormat:@"%.1fKB/s", (double)bytes / 1024];
    }
    else if(bytes >= 1024 * 1024 && bytes < 1024 * 1024 * 1024)   // MB
    {
        return [NSString stringWithFormat:@"%.2fMB", (double)bytes / (1024 * 1024)];
    }
    else    // GB
    {
        return [NSString stringWithFormat:@"%.3fGB", (double)bytes / (1024 * 1024 * 1024)];
    }
}

//
- (long long int)getWifiBytes
{
    struct ifaddrs *ifa_list = 0, *ifa;
    if (getifaddrs(&ifa_list) == -1)
    {
        return 0;
    }
    
    uint32_t iBytes = 0;
    uint32_t oBytes = 0;
    
    for (ifa = ifa_list; ifa; ifa = ifa->ifa_next)
    {
        if (AF_LINK != ifa->ifa_addr->sa_family)
            continue;
        
        if (!(ifa->ifa_flags & IFF_UP) && !(ifa->ifa_flags & IFF_RUNNING))
            continue;
        
        if (ifa->ifa_data == 0)
            continue;
        
        /* Not a loopback device. */
        if (strncmp(ifa->ifa_name, "lo", 2))
        {
            struct if_data *if_data = (struct if_data *)ifa->ifa_data;
            
            iBytes += if_data->ifi_ibytes;
            oBytes += if_data->ifi_obytes;
            
            //            NSLog(@"%s :iBytes is %d, oBytes is %d",
            //                  ifa->ifa_name, iBytes, oBytes);
        }
    }
    freeifaddrs(ifa_list);
    
    return iBytes+oBytes;  
}


- (long long int) getGprsBytes
{
    struct ifaddrs *ifa_list= 0, *ifa;
    if (getifaddrs(&ifa_list)== -1)
    {
        return 0;
    }
    
    uint32_t iBytes =0;
    uint32_t oBytes =0;
    
    for (ifa = ifa_list; ifa; ifa = ifa->ifa_next)
    {
        if (AF_LINK!= ifa->ifa_addr->sa_family)
            continue;
        
        if (!(ifa->ifa_flags& IFF_UP) &&!(ifa->ifa_flags& IFF_RUNNING))
            continue;
        
        if (ifa->ifa_data== 0)
            continue;
        
        if (!strcmp(ifa->ifa_name,"pdp_ip0"))
        {
            struct if_data *if_data = (struct if_data*)ifa->ifa_data;
            
            iBytes += if_data->ifi_ibytes;
            oBytes += if_data->ifi_obytes;
            NSLog(@"%s :iBytes is %d, oBytes is %d",ifa->ifa_name, iBytes, oBytes);
        }
    }
    freeifaddrs(ifa_list);
    
    return iBytes + oBytes;  
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值