3

I am creating custom video player using AVPlayer in ios (OBJECTIVE-C).I have a settings button which on clicking will display the available video dimensions and audio formats. Below is the design:

enter image description here

so,I want to know:

1).How to get the available dimensions from a video url(not a local video)?

2). Even if I am able to get the dimensions,Can I switch between the available dimensions while playing in AVPlayer?

Can anyone give me a hint?

abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70

2 Answers2

5

If it is not HLS (streaming) video, you can get Resolution information with the following code.

Sample code:

// player is playing
if (_player.rate != 0 && _player.error == nil)
{
    AVAssetTrack *track = [[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo] firstObject];
    if (track != nil)
    {
        CGSize naturalSize = [track naturalSize];
        naturalSize = CGSizeApplyAffineTransform(naturalSize, track.preferredTransform);

        NSInteger width = (NSInteger) naturalSize.width;
        NSInteger height = (NSInteger) naturalSize.height;
        NSLog(@"Resolution : %ld x %ld", width, height);
    }
}

However, for HLS video, the code above does not work. I have solved this in a different way. When I played a video, I got the image from video and calculated the resolution of that.

Here is the sample code:

// player is playing
if (_player.rate != 0 && _player.error == nil)
{
    AVAssetTrack *track = [[_player.currentItem.asset tracksWithMediaType:AVMediaTypeVideo] firstObject];
    CMTime currentTime = _player.currentItem.currentTime;
    CVPixelBufferRef buffer = [_videoOutput copyPixelBufferForItemTime:currentTime itemTimeForDisplay:nil];

    NSInteger width = CVPixelBufferGetWidth(buffer);
    NSInteger height = CVPixelBufferGetHeight(buffer);
    NSLog(@"Resolution : %ld x %ld", width, height);
}
hooni
  • 249
  • 3
  • 9
0

As you have mentioned the that it is not a local video, you can call on some web service to return the available video dimensions for that particular video. After that change the URL to other available video and seek to the current position.

Refer This

Community
  • 1
  • 1
Ali M Irshad
  • 116
  • 1
  • 8
  • Thanks for the response.But in case of Hls videos ,there might be different resolutions available .Actually iOS will automatically handle them based on the network speed I guess.But I am asking if we can retrieve those data and switch between them.I am talking about a single video url.Different streams might be available in on single url. – abhimuralidharan Apr 13 '16 at 10:53