roland
roland

Reputation: 7775

iPhone - Streaming Audio

I'm new to Mac Dev and I'm struggling with audio streaming. I found out a good reference at Get an audio stream from URI and play it on iPhone

The author seemed happy and I tried to reproduce the solution. The stream comes from Dogglounge radio. The URL is http://dl3.mixcache.com:8000 (this is what iTunes returns when I click on Get Info).

What I did is create a iPhone app, which contains a single view with no control actually. I added code in viewDidLoad to launch the stream asap. It seems NSData *_objectData runs endlessly. I use the iOS6 simulator.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString* resourcePath = @"http://dl3.mixcache.com:8000"; //your url
    NSURL *url = [NSURL URLWithString:resourcePath];

    NSError *e = nil;
    NSData *_objectData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&e];


    NSLog(@"%@", [e localizedDescription]);

    NSError *error;

    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
    audioPlayer.numberOfLoops = 0;
    audioPlayer.volume = 1.0f;
    [audioPlayer prepareToPlay];

    if (audioPlayer == nil)
        NSLog(@"%@", [error description]);
    else
        [audioPlayer play];


}

Upvotes: 2

Views: 875

Answers (1)

David M.
David M.

Reputation: 3685

This is the expected behavior of NSData dataWithContentsOfURL:: it blocks until the data is downloaded. For asynchronous downloading, see this answer: Does -dataWithContentsOfURL: of NSData work in a background thread?

Upvotes: 1

Related Questions