iOS:如何知道现场音频流是否“播出”?

时间:2012-06-11 14:22:33

标签: ios avplayer live-streaming

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

我做了我的作业,但没有找到关于那个主题的任何内容......

非常感谢...

1 个答案:

答案 0 :(得分: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];
    }
}

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