通过AvPlayerItem和AvPlayer - iOS进行流式传输时监控下载的字节数

时间:2012-10-21 16:19:32

标签: ios stream mp3 radio

我希望在从URL流式传输mp3并将其打印到IULabel时监控下载的字节数。我找不到任何简单的方法来做到这一点。

“AVPlayerItemAccessLog”怎么样,它有信息吗?我无法弄清楚如何使用它?有人知道如何获得这些信息吗?

以下是播放视频流的代码:

-(void)play
{
    /*Allow radio to run in background*/
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil]; 

    /*Gets the url fra sender*/
    NSString *urlString=  self.radioStation.url;
    NSURL *url = [NSURL URLWithString:urlString];

    /*Stop the radio if its play or have info*/
    if(self.avPlayerItem)
    {
        [self stop];

    }


    /*Sets the Avplayeritem*/
    self.avPlayerItem = [AVPlayerItem playerItemWithURL:url];

    /*Listen for changes in the avPlayerItem*/    
    [self.avPlayerItem addObserver:self forKeyPath:@"status" options:0 context:nil];   

    /*Sets the avplayer with avplayeritem*/
    self.avPlayer = [AVPlayer playerWithPlayerItem:self.avPlayerItem];

    /*Sends loading info to the nortification*/
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Loading" object:self];

    /*Plays the radio*/
    [self.avPlayer play];
}

1 个答案:

答案 0 :(得分:0)

您需要在AVPlayerItem上使用KVO来获取此信息。像这样:

[self.player.currentItem addObserver:self
                          forKeyPath:@"loadedTimeRanges"
                             options:NSKeyValueObservingOptionNew
                             context:NULL];

然后是标准的观察者方法:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    // Get the player item's loadedTimeRanges value here.
}

在释放播放器项目之前,不要忘记删除观察者。