我是Mac Dev的新手,我正在努力学习音频流。我在Get an audio stream from URI and play it on iPhone
找到了一个很好的参考作者似乎很开心,我试图重现解决方案。小溪来自Dogglounge电台。该网址为http://dl3.mixcache.com:8000(这是iTunes点击“获取信息”时返回的内容)。
我所做的是创建一个iPhone应用程序,其中包含一个实际上没有控件的视图。我在viewDidLoad中添加了代码以尽快启动流。似乎NSData * _objectData无休止地运行。我使用iOS6模拟器。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString* resourcePath = @"http://dl3.mixcache.com:8000"; //your url
NSURL *url = [NSURL URLWithString:resourcePath];
NSError *e = nil;
NSData *_objectData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&e];
NSLog(@"%@", [e localizedDescription]);
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog(@"%@", [error description]);
else
[audioPlayer play];
}
答案 0 :(得分:1)
这是NSData dataWithContentsOfURL:
的预期行为:它会一直阻塞,直到下载数据为止。对于异步下载,请参阅以下答案:Does -dataWithContentsOfURL: of NSData work in a background thread?