How to play a music file using the URL link in iPhone?

I want to play a music file using the URL link in the iPhone. But when I use the code below, I get an error. I do not understand where I am mistaken. Can anyone fix me?

-(void)viewDidLoad { [super viewDidLoad]; NSString* resourcePath = @"http://192.167.1.104:8888/SHREYA.mp3"; //your url NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]]; NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:_objectData error:&error]; audioPlayer.numberOfLoops = -1; if (audioPlayer == nil) NSLog([error description]); else [audioPlayer play]; } 
+6
source share
5 answers

This should work:

 NSURL *url = [NSURL URLWithString:@"<#Live stream URL#>]; // You may find a test stream at <http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8>. self.playerItem = [AVPlayerItem playerItemWithURL:url]; //(optional) [playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext]; self.player = [AVPlayer playerWithPlayerItem:playerItem]; self.player = [AVPlayer playerWithURL:<#Live stream URL#>]; //(optional) [player addObserver:self forKeyPath:@"status" options:0 context:&PlayerStatusContext]; 

Use the following code to play music:

[self.player play];

+14
source

Try the following:

 -(void)viewDidLoad { [super viewDidLoad]; NSString* resourcePath = @"your url"; //your url NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]]; NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error]; audioPlayer.numberOfLoops = 1; if (audioPlayer == nil) NSLog([error description]); else [audioPlayer play]; 
+5
source

Current Answer:

Now consider Why AVAudioPlayer initWithContentsOfURL always works with error code = -43 . his solution avplayer

Previous answer

 //You have to pass NSURL not NSData NSString* resourcePath = @"http://192.167.1.104:8888/SHREYA.mp3"; //your url NSURL *url = [NSURL alloc]initWithString:resourcePath]; audioPlayer = [[AVAudioPlayer alloc] initWithURL:url error:&error]; audioPlayer.delegate = self; [url release]; [audioPlayer prepareToPlay]; [audioPlayer play]; 
0
source

Try my code:

 NSURL *filepath = [NSURL fileURLWithPath:audioFilePath]; [yourMediaPlayer setContentURL:filepath]; //set player. yourMediaPlayer.controlStyle = MPMovieControlStyleNone; yourMediaPlayer.currentPlaybackTime = 0; yourMediaPlayer.shouldAutoplay = FALSE; [yourMediaPlayer play]; 

^ - ^, I am using MPMoviePlayerController with the above code.

0
source

I assume you send

 audioPlayer.numberOfLoops = -1; 

to get an NSInvalidArgumentException, change it to

 audioPlayer.numberOfLoops = 1; 

and check out the documentation on Apple here

0
source

Source: https://habr.com/ru/post/921638/


All Articles