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

情到浓时终转凉″ 提交于 2019-12-17 21:57:14

问题


I want to play a music file using URL Link in the iPhone. But when I use the below code I am getting error I am not getting where I am going wrong. Can anyone Correct 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];
}

回答1:


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




回答2:


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



回答3:


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.




回答4:


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



回答5:


I guess you are sending

audioPlayer.numberOfLoops = -1;

to receive NSInvalidArgumentException change it to

audioPlayer.numberOfLoops = 1;

and check apple's documentation here



来源:https://stackoverflow.com/questions/11716244/how-to-play-a-music-file-using-url-link-in-iphone

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