self.player = [[AVPlayer playerWithURL:[NSURL URLWithString:@"http://myurl.com/track.mp3"]] retain];
上記のトラックの UIProgressView を作成しようとしています。その URL からファイル サイズと現在のファイル サイズを取得するにはどうすればよいですか? 助けてください、ありがとう!
self.player = [[AVPlayer playerWithURL:[NSURL URLWithString:@"http://myurl.com/track.mp3"]] retain];
上記のトラックの UIProgressView を作成しようとしています。その URL からファイル サイズと現在のファイル サイズを取得するにはどうすればよいですか? 助けてください、ありがとう!
次のように、現在のアイテムの 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
}
@"loadedTimeRange" は、AVPlayerItem クラスの KVO 値です。その定義は、次の AVPlayerItem.h ファイルにあります。
@interface AVPlayerItem (AVPlayerItemPlayability)
カテゴリー定義。
ファイルサイズについては何も知りたくないと思いますが、時間の方が興味があります。
self.player.currentItem.asset.duration
現在再生中のアイテムの継続時間、現在の時刻を試しself.player.currentTime
ます。