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.

2
  • stackoverflow.com/questions/3275990/… possible help Aug 11, 2012 at 0: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, 2012 at 10:53

1 Answer 1

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];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.