title: Get an audio stream from URI and play it on iPhone
body: <p>I would like to get an audio stream from URL and play it on iPhone. Unfortunately, there is only C# example of how to play that stream, but I need a realization on Objective C in iPhone frameworks. I have tried my self to use different audio frameworks but it was all unsuccessful. This is what in C#: </p>
  
  <pre><code>response = httpWebRequest.GetResponse();
              using (Stream stream = response.GetResponseStream())
              {
                  using (SoundPlayer player = new SoundPlayer(stream))
                  {
                      player.PlaySync();
                  }
              }
  </code></pre>
  
  <p>What should I do in objective C to get a stream audio from that url? In that URL I don't have an mp3 file or something, I just get a stream. Probably I must somehow download that stream to an iPhone audio file and then play it to get rid of lags. </p>
  
  <p>I tried this: </p>
  
  <pre><code> AVPlayer *player = [AVPlayer playerWithURL:url];
   [player play];
  </code></pre>
  
  <p>And this: </p>
  
  <pre><code>AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:nil];
  
      [theAudio setVolume :0.50]; //setting the volume
  
      [theAudio play];
  </code></pre>
  
  <p>Neither worked. 
  I have tested my url, it works well in browser.</p>
  
  <p>This is the code that worked: </p>
  
  <pre><code>NSData *_objectData = [NSData dataWithContentsOfURL:url];
      NSError *error;
  
      AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&amp;error];
      audioPlayer.numberOfLoops = 0;
      audioPlayer.volume = 1.0f;
      [audioPlayer prepareToPlay];
  
      if (audioPlayer == nil)
          NSLog(@"%@", [error description]);
      else
          [audioPlayer play];
  </code></pre>