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

ⅰ亾dé卋堺 提交于 2019-11-28 17:20:33
iPhone Developer

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 the music:

[self.player play];

Aklesh Rathaur

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];

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 use MPMoviePlayerController with above code.

Paresh Navadiya

Current Answer:

Now consider Why does AVAudioPlayer initWithContentsOfURL always fail with error code=-43 link. its solution is avplayer link

Earlier 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];

I guess you are sending

audioPlayer.numberOfLoops = -1;

to receive NSInvalidArgumentException change it to

audioPlayer.numberOfLoops = 1;

and check apple's documentation here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!