Play audio from the Internet using AVAudioPlayer

I use AVAudioPlayer to play sound, and it works great when playing files locally stored on a PC.

But when I give the URL of some audio file over the Internet, it fails. This is what the code looks like:

NSString *url = [[NSString alloc] init]; url = @"http://files.website.net/audio/files/audioFile.mp3"; NSURL *fileURL = [[NSURL alloc] initWithString: url]; AVAudioPlayer *newPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil]; 

Can someone point out the problem and what can be done?
Thank!

+6
ios objective-c streaming avaudioplayer
03 Sep '10 at 12:40
source share
4 answers

I tried another initWithData method on AVAudioPlayer instead of initWithContentsOfURL. First try to get the MP3 file in NSData, and then play this data.

Take a look at my code here .

+2
Sep 22 '10 at 9:51
source share

Use AVPlayer for streaming audio / video based on http url. It will work fine. AVAudioPlayer is for local files. Here is the code

 NSURL *url = [NSURL URLWithString:url]; self.avAsset = [AVURLAsset URLAssetWithURL:url options:nil]; self.playerItem = [AVPlayerItem playerItemWithAsset:avAsset]; self.audioPlayer = [AVPlayer playerWithPlayerItem:playerItem]; [self.audioPlayer play]; 
+27
Apr 29 '13 at 9:45
source share

This is what Apple reports:

The AVAudioPlayer class AVAudioPlayer not provide audio streaming support based on HTTP URLs. The URL used with initWithContentsOfURL: must be the file URL ( file:// ). That is the local path.

+17
Aug 04 2018-11-11T00:
source share

Use AVPlayer and track its status to start playback.

Here is an example of working capacity, I hope that it will be useful.

 @implementation AudioStream - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context { if (context == PlayerStatusContext) { AVPlayer *thePlayer = (AVPlayer *)object; switch ([thePlayer status]) { case AVPlayerStatusReadyToPlay: NSLog(@"player status ready to play"); [thePlayer play]; break; case AVPlayerStatusFailed: NSLog(@"player status failed"); break; default: break; } return; } else if (context == ItemStatusContext) { AVPlayerItem *thePlayerItem = (AVPlayerItem *)object; switch ([thePlayerItem status]) { case AVPlayerItemStatusReadyToPlay: NSLog(@"player item ready to play"); break; case AVPlayerItemStatusFailed: NSLog(@"player item failed"); break; default: break; } return; } [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } - (void)playAudioStream { NSURL *audioUrl = [NSURL URLWithString:@"your_stream_url"]; AVURLAsset *audioAsset = [AVURLAsset assetWithURL:audioUrl]; AVPlayerItem *audioPlayerItem = [AVPlayerItem playerItemWithAsset:audioAsset]; [audioPlayerItem addObserver:self forKeyPath:@"status" options:0 context:ItemStatusContext]; self.player = [AVPlayer playerWithPlayerItem:audioPlayerItem]; [self.player addObserver:self forKeyPath:@"status" options:0 context:PlayerStatusContext]; } @end 
0
May 03 '17 at 1:40 a.m.
source share



All Articles