由于其他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;
}