对于 HLS 流式视频,代码不起作用。
[_player.currentItem.asset tracking] 是一个空数组。
所以“[_player.currentItem.asset trackWithMediaType:AVMediaTypeVideo]”也返回零。
我用不同的方式和你解决了同样的问题。
我从流视频中获取图像并计算分辨率以获得宽度和高度。
下面的代码工作正常:
if ((_player.rate != 0) && (_player.error == nil)) // player is playing
{
AVAssetTrack *track = [[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo] firstObject];
if (track != nil) // not HLS
{
CGSize naturalSize = [track naturalSize];
naturalSize = CGSizeApplyAffineTransform(naturalSize, track.preferredTransform);
_videoWidth = (NSInteger) naturalSize.width;
_videoHeight = (NSInteger) naturalSize.height;
}
else // HLS!!
{
CMTime currentTime = _player.currentItem.currentTime;
CVPixelBufferRef buffer = [_videoOutput copyPixelBufferForItemTime:currentTime itemTimeForDisplay:nil];
_videoWidth = CVPixelBufferGetWidth(buffer);
_videoHeight = CVPixelBufferGetHeight(buffer);
}
}
NSLog(@"Resolution : %ld x %ld", _videoWidth, _videoHeight);