2
我尝试了以下三种解决方案,但都没有工作。我相信我错过了一些关键点,但不清楚是什么。 (仅供参考,我在iPhone设备上尝试以下解决方案,而不是在模拟器。)从网络播放mp3
- (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];
}
任何帮助深表感谢。
http://stackoverflow.com/questions/3275990/iphone-sdk-support-for-playing-mp3-files-over-the-network可能的帮助 – 2012-08-11 00:09:24
很多后解决问题和花费时间,最后解决问题。它变得非常非常简单。变量范围。在我所有的三种方法中,我在方法中定义了玩家实例变量,只要方法执行完成,玩家变量引用计数就被设置为ZERO(通过ARC)。所以我定义了一个类级别的变量,赋予了它的实例变量并且一切正常。 – Shas 2012-08-13 10:53:27