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