2009-12-29 123 views
2

我想播放发布在网站上的音频文件。我正在尝试使用AVAudioPlayer并将网址设置为其网站链接。但bgPlayer原来是零。我想我可以下载这个音频文件并在之后播放,但有什么方法可以在我下载时播放它?如何在iPhone上的网站上播放音频文件?

NSURL *bgURL = [NSURL URLWithString:@"http://www.radioslots.com/iphone/test/tmp/1.mp3"]; 
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:nil]; 
bgPlayer.numberOfLoops = -1; 
if (bgPlayer) { 
    [bgPlayer play]; 
} 

回答

1

AVAudioPlayer没有打开网络流,我可能会误解,但我很确定这是不可能的。

就在我头顶,我可以想到两种可能的方法: 1.使用MPMoviePlayer,因为这可以打开网络流,尽管该名称可以与音频一起使用。唯一的一点是这会弹出带有控件的模式播放器。 2.下载网络文件并将其存储在本地,然后使用AVAudioPlayer播放本地文件。

+0

谢谢。 MPMoviePlayer可以像您提到的那样在网站上播放mp3。 – 2010-05-21 06:44:10

1

我刚才写的答案here。检查并更换您使用那里的网址..

您只需更换

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"songname" ofType:@"mp3"]; 
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 

NSString *soundFilePath = @"Your URL"; 
NSURL *newURL = [[NSURL alloc] initWithString: soundFilePath]; 
+1

但我想在网站上播放音频文件,而不是从本地文件播放。 – 2009-12-29 10:49:10

0

有一个适当的文件可在this link

但我认为你还应该检查你通过这种方式得到的错误代码:

NSError *error; 
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:&error]; 

希望这有助于。