2

我尝试了以下三种解决方案,但都没有奏效。我确信我错过了一些关键点,但不清楚是什么。(仅供参考,我正在 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];
}

任何帮助深表感谢。

4

1 回答 1

0

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

MPMoviePlayerController *myAudioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:urlTextField.text]];
[myAudioPlayer play];
于 2012-08-11T08:11:22.190 回答