1

我正在开发一个必须流式传输实时音频(来自 m3u 远程文件)的应用程序,并且我正在寻找一种方法来检查实时流是“播出”还是“播出”。音频播放器使用AVPlayer.

我做了功课,但没有找到关于那个主题的任何东西......

非常感谢...

4

1 回答 1

2

当你使用 AVPlayer 和 AVPlayerItem 时,像下面这样添加观察者:

-(void) addMediaObservers {
    [_playerItem addObserver:self forKeyPath:@"player_buffer_empty" options:0 context:@"player_buffer_empty"];
    [_playerItem addObserver:self forKeyPath:@"item_status" options:0 context:@"item_status"];

    [_player addObserver:self forKeyPath:@"player_status" options:0 context:@"player_status"];
}

请不要忘记在停止流式传输时或在 dealloc 方法中删除这些观察者。

- (void)stop
{
    [_playerItem removeObserver:self forKeyPath:@"player_buffer_empty"];
    [_playerItem removeObserver:self forKeyPath:@"item_status"];
    [_player removeObserver:self forKeyPath:@"player_status"];
}

在以下方法中,您将管理音频流:

- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context
{
    if (context && ([context isEqualToString:@"item_status"] || [context isEqualToString:@"player_status"] || [context isEqualToString:@"player_buffer_empty"]))
    {
        [self checkStatus];
    }
}

- (void)checkStatus
{
    AVPlayerItemStatus ps = _playerItem && _playerItem.status ? _playerItem.status : AVPlayerItemStatusUnknown;
    AVPlayerStatus s = _player && _player.status ? _player.status : AVPlayerStatusUnknown;

    BOOL isReady = ps == AVPlayerItemStatusReadyToPlay && s == AVPlayerStatusReadyToPlay;

    if (_isPlaying) {
        if (!_isLoading && _player && _playerItem && _playerItem.playbackBufferEmpty) {
            _isLoading = YES;
            [self performSelector:@selector(unpause) withObject:nil afterDelay:20];
        }
        if (!isReady)
            [self stop];
    } else {
        if (isReady)
            [self play];
    }
}

方法范围内未声明的方法中使用的所有变量都是全局变量。我希望这有帮助!

于 2012-06-11T15:34:57.873 回答