self.player = [[AVPlayer playerWithURL:[NSURL URLWithString:@"http://myurl.com/track.mp3"]] retain];
我正在嘗試爲上述軌道製作UIProgressView。如何從該URL獲取文件大小和當前文件大小?請幫忙,謝謝!如何從NSURL獲取AVPlayer的文件大小和當前文件大小iOS4.0
self.player = [[AVPlayer playerWithURL:[NSURL URLWithString:@"http://myurl.com/track.mp3"]] retain];
我正在嘗試爲上述軌道製作UIProgressView。如何從該URL獲取文件大小和當前文件大小?請幫忙,謝謝!如何從NSURL獲取AVPlayer的文件大小和當前文件大小iOS4.0
我認爲你不想知道關於文件大小的任何事情,但你對時代更感興趣。
嘗試self.player.currentItem.asset.duration
持續當前播放項目,self.player.currentTime
當前時間。
你需要開始觀察當前項目的loadedTimeRanges財產,像這樣:
AVPlayerItem* playerItem = self.player.currentItem;
[playerItem addObserver:self forKeyPath:kLoadedTimeRanges options:NSKeyValueObservingOptionNew context:playerItemTimeRangesObservationContext];
然後,在觀察回調,你讓你通過這樣的數據的意義:
-(void)observeValueForKeyPath:(NSString*)aPath ofObject:(id)anObject change:(NSDictionary*)aChange context:(void*)aContext {
if (aContext == playerItemTimeRangesObservationContext) {
AVPlayerItem* playerItem = (AVPlayerItem*)anObject;
NSArray* times = playerItem.loadedTimeRanges;
// there is only ever one NSValue in the array
NSValue* value = [times objectAtIndex:0];
CMTimeRange range;
[value getValue:&range];
float start = CMTimeGetSeconds(range.start);
float duration = CMTimeGetSeconds(range.duration);
_videoAvailable = start + duration; // this is a float property of my VC
[self performSelectorOnMainThread:@selector(updateVideoAvailable) withObject:nil waitUntilDone:NO];
}
然後在主線程中選擇更新進度條,就像這樣:
-(void)updateVideoAvailable {
CMTime playerDuration = [self playerItemDuration];
double duration = CMTimeGetSeconds(playerDuration);
_videoAvailableBar.progress = _videoAvailable/duration;// this is a UIProgressView
}
kLoadedTimeRanges應該代表什麼?我無法在任何地方找到這個 – 2012-01-25 00:20:41
只是爲了防止複製和粘貼錯誤。確保你在count 8時測試times數組。在iOS 8中,它可以返回一個空數組。 – 2014-09-19 17:26:12
@「loadedTimeRange」是AVPlayerItem類的KVO值。你可以在AVPlayerItem.h文件中找到它的定義,在
@interface AVPlayerItem (AVPlayerItemPlayability)
類別定義。
是的,我製作滑塊的持續時間。但是,如果用戶幻燈片通過了什麼是加載的,那麼壞消息。所以我想要下載/播放曲目的進度,是嗎?那有意義嗎? – slowman21 2010-10-22 22:08:56
我明白了,我不確定這一點,但是您可能希望僅在「self.player.currentItem.loadedTimeRanges」中指定的範圍內啓用搜索。 – Michal 2010-10-23 08:49:57
是的,我也需要這樣做。但仍然會讓用戶看到已加載的內容。就像一個真正的流光。 – slowman21 2010-10-25 14:48:43