2016-04-13 90 views
3

我使用AVPlayer在ios中創建自定義視頻播放器(OBJECTIVE-C)。我有一個設置按鈕,點擊時會顯示可用的視頻尺寸和音頻格式。 下面是設計:如何從視頻網址iOS獲取可用的視頻尺寸/質量?

enter image description here

所以,我想知道:

1)。如何得到一個視頻網址(不是本地視頻)可用的尺寸是多少?

2)。即使我能夠獲得尺寸,在AVPlayer中播放時,是否可以在可用尺寸之間切換?

任何人都可以給我一個提示嗎?

回答

1

正如您所提及的,它不是本地視頻,您可以調用某個Web服務來返回該特定視頻的可用視頻尺寸。之後,將URL更改爲其他可用視頻並尋找當前位置。

Refer This

+0

感謝response.But在HLS視頻的情況下,可能有不同的分辨率提供。實際上,iOS將自動處理這些基於網絡速度我guess.But我問我們是否可以檢索這些數據並在它們之間切換。我正在談論一個視頻url。不同的流可能在單個url上可用。 – abhi1992

4

如果不是HLS(流)的視頻,你可以用下面的代碼解決方案的信息。

示例代碼:

// 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); 
    } 
} 

然而,HLS視頻,上面的代碼不起作用。 我以不同的方式解決了這個問題。 當我播放視頻時,我從視頻中獲得圖像並計算出該圖像的分辨率。

下面是示例代碼:

// 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); 
} 
+0

如何在youtube之間切換流? – abhi1992