0

我想在从 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];
}
4

1 回答 1

-1

您需要在 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.
}

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

于 2013-04-16T22:58:04.510 回答