1

I am trying to play mp3 file located somewhere on remote server.I have the url of that music fileI have tried it playing it using AVAudioPlayer or AVPlayer but i am not able to play the mp3 file.Please tell me how can i play the mp3 file.

code to play music:

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

Please tell me how do i do it?

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
TechChain
  • 8,404
  • 29
  • 103
  • 228

2 Answers2

4

Try Below Code.

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];
poojathorat
  • 1,200
  • 2
  • 9
  • 19
  • How can i show seekbar & other controls for this.Please suggesst? – TechChain Aug 17 '15 at 06:25
  • You can map the duration of the file being played by the AVPlayer to the UISlider's range (maximum and minimum values) using the duration property of AVPlayer and make changes in the slider position to set the currentTime of the AVPlayer. – poojathorat Aug 17 '15 at 06:28
  • do you have some sample code or may be a link for that – TechChain Aug 17 '15 at 06:30
  • http://stackoverflow.com/questions/8725505/timeline-progress-bar-for-avplayer refer this link – poojathorat Aug 17 '15 at 06:31
  • i mean it plays your given link url , but it does not play my mp3 url . what should be the problem there ? – Moxarth Sep 21 '17 at 06:40
0

Try use AVAudioPlayer, see below

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

Hope it works.

Cheers.

iphonic
  • 12,615
  • 7
  • 60
  • 107