使用AVPlayer在ios中进行audiostream式传输

我正在试图开发一个小应用程序,应该从特定的链接streamaudio。 我正在使用AVPlayer,但我不知道为什么它不工作。 尝试谷歌,但似乎没有任何相关的。 请帮助..谢谢

- (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL URLWithString:@"http://108.166.161.206:8826/stream.mp3"]; self->playerItem = [AVPlayerItem playerItemWithURL:url]; self->player = [AVPlayer playerWithPlayerItem:playerItem]; self->player = [AVPlayer playerWithURL:url]; [player play]; } 

不知道为什么你先用playeritem初始化你的播放器,然后用NSURL初始化它。 无论如何,这是要走的路:

 -(void) viewDidLoad{ player = [[AVPlayer alloc] initWithURL: [NSURL URLWithString:@"http://xxx/yourfile.mp3"]]; [player addObserver:self forKeyPath@"status" options:0 context:nil]; } - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (object == player && [keyPath isEqualToString:@"status"]) { if (player.status == AVPlayerStatusReadyToPlay) { [player play]; } if (player.status == AVPlayerStatusFailed) { NSLog(@"Something went wrong: %@", player.error); } } } 

AV Foundation编程指南应该是了解AVPlayer工作原理的一个很好的起点

你可以用AVAudioPlayer来试试这个:

 NSData *fileData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://108.166.161.206:8826/stream.mp3"]]; NSError *error = nil; self.player = [[AVAudioPlayer alloc] initWithData:fileData error:&error]; [self.player play];