Используйте AVPlayer
и следите за его состоянием, чтобы начать воспроизведение.
Вот рабочий пример, надеюсь, он будет полезен.
@implementation AudioStream
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if (context == PlayerStatusContext) {
AVPlayer *thePlayer = (AVPlayer *)object;
switch ([thePlayer status]) {
case AVPlayerStatusReadyToPlay:
NSLog(@"player status ready to play");
[thePlayer play];
break;
case AVPlayerStatusFailed:
NSLog(@"player status failed");
break;
default:
break;
}
return;
} else if (context == ItemStatusContext) {
AVPlayerItem *thePlayerItem = (AVPlayerItem *)object;
switch ([thePlayerItem status]) {
case AVPlayerItemStatusReadyToPlay:
NSLog(@"player item ready to play");
break;
case AVPlayerItemStatusFailed:
NSLog(@"player item failed");
break;
default:
break;
}
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (void)playAudioStream
{
NSURL *audioUrl = [NSURL URLWithString:@"your_stream_url"];
AVURLAsset *audioAsset = [AVURLAsset assetWithURL:audioUrl];
AVPlayerItem *audioPlayerItem = [AVPlayerItem playerItemWithAsset:audioAsset];
[audioPlayerItem addObserver:self forKeyPath:@"status" options:0 context:ItemStatusContext];
self.player = [AVPlayer playerWithPlayerItem:audioPlayerItem];
[self.player addObserver:self forKeyPath:@"status" options:0 context:PlayerStatusContext];
}
@end