Search code examples
iosobjective-ciphoneavaudioplayerinternet-radio

How to play a radio station on button press in iOS


I want to play a radio station when someone presses the play radio button. However, it does not work.

Here is my code:

NSURL *url = [NSURL URLWithString:@"http://www.radio.net.pk/"];

NSData *data = [NSData dataWithContentsOfURL:url];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audio play];

Solution

  • The AVAudioPlayer class only lets you play sound in any audio format available in iOS and OS X

    This means, the AVAudioPlayer class does not provide support for streaming audio based on HTTP URL's. The URL used with initWithContentsOfURL: must be a local path URL.

    So you should save the NSData locally (with different chunks) and play it using AVAudioPlayer.