gpt4 book ai didi

iOS : how to know if a live audio stream is "On air"?

转载 作者:行者123 更新时间:2023-11-29 11:12:18 26 4
gpt4 key购买 nike

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

我做了我的家庭作业,但没有找到关于那个主题的任何东西......

非常感谢...

最佳答案

当你使用 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];
}
}

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

关于iOS : how to know if a live audio stream is "On air"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10981980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com