6

我想在 iPhone 中使用 URL Link 播放音乐文件。但是,当我使用下面的代码时,我得到了错误,我没有得到错误的地方。谁能纠正我?

-(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];
}
4

5 回答 5

14

这应该有效:

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

使用以下代码播放音乐:

[self.player play];

于 2012-07-30T07:03:12.440 回答
5

尝试以下操作:

-(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];
于 2013-09-20T06:20:05.317 回答
0

试试我的代码:

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

  [yourMediaPlayer play];

^-^,我在上面的代码中使用 MPMoviePlayerController。

于 2012-07-30T07:19:18.670 回答
0

当前答案:

现在考虑为什么 AVAudioPlayer initWithContentsOfURL 总是失败,错误代码=-43链接。它的解决方案是avplayer链接

较早的答案

//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];
于 2012-07-30T06:28:00.693 回答
0

我猜你正在发送

audioPlayer.numberOfLoops = -1;

接收 NSInvalidArgumentException 将其更改为

audioPlayer.numberOfLoops = 1;

并在此处查看苹果的文档

于 2014-01-07T09:11:28.270 回答