从网络播放mp3

时间:2012-08-11 00:04:31

标签: iphone

我尝试了以下三种解决方案,但没有一种方法有效。我确信我错过了一些关键点,但不清楚是什么。 (仅供参考,我在iPhone设备上尝试以下解决方案,而不是在模拟器上。)

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

尝试使用MPMoviePlayerController。首先,您需要导入MediaPlayer框架。然后就这么简单:

MPMoviePlayerController *myAudioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:urlTextField.text]];
[myAudioPlayer play];