7

I am implementing AVAudioPlayer to play audio and it works perfectly well while playing files locally stored in the PC.

But when i give the url of some audio file over the internet, it fails sadly. Here's what the code looks like:

NSString *url = [[NSString alloc] init];  
url = @"http://files.website.net/audio/files/audioFile.mp3";  
NSURL *fileURL = [[NSURL alloc] initWithString: url];  
AVAudioPlayer *newPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];

Could anybody please point out the problem and what could be done?
Thanks!

Ketan P
  • 4,259
  • 3
  • 30
  • 36
bangdel
  • 2,523
  • 5
  • 28
  • 42

4 Answers4

32

Use AVPlayer to stream audio/video based on http url's. It will work fine. AVAudioPlayer is for local files. Here's the code

NSURL *url = [NSURL URLWithString:url];    
self.avAsset = [AVURLAsset URLAssetWithURL:url options:nil];    
self.playerItem = [AVPlayerItem playerItemWithAsset:avAsset];    
self.audioPlayer = [AVPlayer playerWithPlayerItem:playerItem];    
[self.audioPlayer play];
Gautam Jain
  • 2,913
  • 30
  • 25
21

This is what the Apple docs say:

The AVAudioPlayer class does not provide support for streaming audio based on HTTP URL's. The URL used with initWithContentsOfURL: must be a File URL (file://). That is, a local path.

leviathan
  • 11,080
  • 5
  • 42
  • 40
  • 1
    I spent a long time trying to troubleshoot this until I ran into your answer here. Thanks Internet stranger. This helped a lot. – FontFamily May 11 '21 at 20:53
3

I tried other method initWithData on AVAudioPlayer instead of initWithContentsOfURL. First try to get MP3 file into NSData and then play this data.

Look at my code here.

Community
  • 1
  • 1
Borut Tomazin
  • 8,041
  • 11
  • 78
  • 91
0

Use AVPlayer and monitor its status to start playback.

Here is a workable example, hope it would be helpful.

@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
alijandro
  • 11,627
  • 2
  • 58
  • 74