2

I have tried below three solutions but none of them worked. I am sure I am missing some key point but not clear what. (FYI, I am trying out below solutions on iPhone device, not on simulator.)

- (IBAction)actionAVPlayer:(id)sender { 
AVPlayer* player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:urlTextField.text]]; 
[player play];

}

- (IBAction)actionMemoryBuffer1:(id)sender {

  NSError *error;
  NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlTextField.text]];

  AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
  audioPlayer.numberOfLoops = -1;
  audioPlayer.delegate = self;
  audioPlayer.volume = 1.0f;
  [audioPlayer prepareToPlay];

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


- (IBAction)actionMemoryBuffer2:(id)sender {

  NSURLRequest* request = [ NSURLRequest requestWithURL: [NSURL URLWithString:urlTextField.text] ];
  NSError *requestError;
  NSURLResponse *urlResponse = nil;

  NSData *_objectData  = [ NSURLConnection sendSynchronousRequest:request returningResponse: &urlResponse  error: &requestError];

  NSError *error;
  AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
  audioPlayer.numberOfLoops = -1;
  audioPlayer.delegate = self;
  audioPlayer.volume = 1.0f;
  [audioPlayer prepareToPlay];

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

Any help is much appreciated.

Shas
  • 31
  • 3
  • http://stackoverflow.com/questions/3275990/iphone-sdk-support-for-playing-mp3-files-over-the-network possible help – NIlesh Sharma Aug 11 '12 at 00:09
  • After a lot of trouble shooting and spending hours, at last the problem is addressed. It turn out to be very very simple. Variable Scoping. In my all the 3 approaches, I was defining player instance variable within a method so as soon as execution of method is completed, player variable reference count is set to ZERO (by ARC). So I defined a class level variable, assigned player instance variable to it and all went fine. – Shas Aug 13 '12 at 10:53

1 Answers1

0

Try using MPMoviePlayerController. First you need to import the MediaPlayer framework. Then it's as easy as this:

MPMoviePlayerController *myAudioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:urlTextField.text]];
[myAudioPlayer play];
WolfLink
  • 3,308
  • 2
  • 26
  • 44