3

我正在尝试在下载时从 iTunes 商店播放这首歌

为什么下面的代码不播放?

NSLog(@"start");

NSURL *songUrl = [NSURL URLWithString:@"http://a1804.phobos.apple.com/us/r1000/064/Music/v4/9b/b3/c7/9bb3c7dc-a06f-f18c-3e41-2ce1e36f73b4/mzaf_7432104896053262141.aac.m4a"];


NSError* errorVar = nil;
AVAudioPlayer* song = [[AVAudioPlayer alloc] initWithContentsOfURL:songUrl error:&errorVar];
[song prepareToPlay];
[song play];
4

2 回答 2

3

我认为你必须使用 AVPlayer 而不是 AVAudioPlayer。

Apple 文档建议AVAudioPlayer用于播放文件或内存中的音频数据。AVPlayer与本地和远程媒体文件同样适用。

self.player = [AVPlayer playerWithURL:<#Live stream URL#>];
[player addObserver:self forKeyPath:@"status" options:0 context:&PlayerStatusContext];

有关更多信息,请参阅Apple 文档

于 2013-06-09T07:34:11.930 回答
-1

你可以做

// soundUrl can be a remote url, don't need to be a file url
NSData *data = [NSData dataWithContentsOfURL:soundUrl];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error:nil];
于 2014-06-10T10:44:47.517 回答