如何从iPhone上的在线url获取audio

我知道这已经被很多人问过,但我不知道为什么这不是在我的应用程序在X代码和客观的C!

基本上,我想从一个URL的应用程序播放audio。 只要使用苹果网站的testingaudio,我的代码就是:

NSString *playString = @"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"; //Converts songURL into a playable NSURL NSURL *playURL = [NSURL URLWithString:playString]; AVAudioPlayer *backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:playURL error:&error]; if (backgroundMusicPlayer == nil) { NSLog(@"%@", [error description]); } else { [backgroundMusicPlayer prepareToPlay]; [backgroundMusicPlayer play]; } 

它出现以下错误:

错误域= NSOSStatusErrorDomain代码= -43“操作无法完成。(OSStatus错误-43。)

我找不到任何东西,当我谷歌。 有任何想法吗? 我应该尝试另一个audiostream? 还是有没有人有一个例子,工程? 是因为我使用iPhone模拟器而不是真正的iPhone吗?

编辑:

请注意 – 当我说url我的意思是从一个实际的网站,如“ http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8 ”我不想首先下载歌曲,我想要能够播放它,因为它是下载! 谢谢

使用AVPlayer播放来自url的mp3文件

 -(void)playselectedsong{ AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]]; self.songPlayer = player; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[songPlayer currentItem]]; [self.songPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES]; [self.songPlayer play]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (object == songPlayer && [keyPath isEqualToString:@"status"]) { if (songPlayer.status == AVPlayerStatusFailed) { NSLog(@"AVPlayer Failed"); } else if (songPlayer.status == AVPlayerStatusReadyToPlay) { NSLog(@"AVPlayerStatusReadyToPlay"); } else if (songPlayer.status == AVPlayerItemStatusUnknown) { NSLog(@"AVPlayer Unknown"); } } } - (void)playerItemDidReachEnd:(NSNotification *)notification { // code here to play next sound file } 

其非常简单的使用AVAudioPlayer,把对象当作AVAudioPlayer * audioPlayer,

  NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]; NSData *soundData = [NSData dataWithContentsOfURL:url]; audioPlayer = [[AVAudioPlayer alloc] initWithData:soundData error:NULL]; audioPlayer.delegate = self; [audioPlayer play]; 

写下委托方法:

  -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag [audioPlayer stop]; NSLog(@"Finished Playing"); } - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error { NSLog(@"Error occured"); } 

你可以在这里find一个现场testingstream

播放文件使用:

 [player play]; 

这里是从url播放直播的代码:

 NSURL *url = [NSURL URLWithString:@"<#Live stream URL#>]; self.playerItem = [AVPlayerItem playerItemWithURL:url]; /* [playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext]; (can use it) */ self.player = [AVPlayer playerWithPlayerItem:playerItem]; self.player = [AVPlayer playerWithURL:<#Live stream URL#>]; //(optional) [player addObserver:self forKeyPath:@"status" options:0 context:&PlayerStatusContext];