如何从iOS中的url播放mp3文件?

时间:2015-08-17 04:53:02

标签: ios objective-c

我正在尝试播放位于远程服务器某处的mp3文件。我有该音乐文件的网址我尝试使用AVAudioPlayerAVPlayer播放它但我无法播放mp3 file.Please告诉我怎么才能播放mp3文件。

播放音乐的代码:

 NSString *aSongURL = [NSString stringWithFormat:@"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
    // NSLog(@"Song URL : %@", aSongURL);

    AVPlayerItem *aPlayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:aSongURL]];
    AVPlayer *anAudioStreamer = [[AVPlayer alloc] initWithPlayerItem:aPlayerItem];
    [anAudioStreamer play];

    // Access Current Time
    NSTimeInterval aCurrentTime = CMTimeGetSeconds(anAudioStreamer.currentTime);

    // Access Duration
    NSTimeInterval aDuration = CMTimeGetSeconds(anAudioStreamer.currentItem.asset.duration);

请告诉我该怎么做?

2 个答案:

答案 0 :(得分:3)

尝试以下代码。

NSURL *url = [NSURL URLWithString:@"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:url];
[self.player play];

答案 1 :(得分:0)

尝试使用AVAudioPlayer,见下文

NSString *aSongURL = [NSString stringWithFormat:@"http://megdadhashem.wapego.ru/files/56727/tubidy_mp3_e2afc5.mp3"];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:aSongURL] error:nil];

[audioPlayer play];

希望它有效。

干杯。